Swift 中的 UIButton 没有注册触摸

UIButton in Swift is not registering touches(Swift 中的 UIButton 没有注册触摸)
本文介绍了Swift 中的 UIButton 没有注册触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Swift 创建一个 UIButton.它编译得很好,我可以在模拟器中看到我的按钮,但是当我点击它时,什么也没有发生.这是我正在使用的代码:

I'm trying to create a UIButton using Swift. It compiles fine and I can see my button in the simulator, but when I click it, nothing happens. This is the code I am using:

let settings = UIButton()
settings.addTarget(self, action: "touchedSet:", forControlEvents: UIControlEvents.TouchUpInside)
settings.setTitle("Settings", forState: .Normal)
settings.frame = CGRectMake(0, 530, 150, 50)
scrollView.addSubview(settings)

在同一个类中,这里是函数'touchedSet':

In the same class, here is the function 'touchedSet':

func touchedSet(sender: UIButton!) {
    println("You tapped the button")
}

我正在使用模拟器,因为我没有 iOS 8.0 设备,这可能是问题吗?

I'm using the simulator as I don't have an iOS 8.0 device, could that be the problem?

谢谢!

推荐答案

我看到这是一个老问题,但我昨天遇到了非常相似的问题.我的按钮在触摸时突出显示,但未触发操作.

I see it is an old question but I just faced very similar problem yesterday. My buttons were highlighted on touch but not firing the action.

我有两个视图控制器.一个视图覆盖其他视图,按钮位于顶部视图.

I have two view controllers. One view covers the others view and button is on the top view.

例如.

  • Rootviewcontroller 有后视图

  • Rootviewcontroller has back view

Topviewcontroller 有顶视图

Topviewcontroller has top view

如果我不将topviewcontroler 添加为rootviewcontroller 的childviewcontroller,顶视图上的按钮不会调用该操作.将 topviewcontroller 添加为 childviewcontroller 后,它开始工作.

The button on top view does not call the action if I don't add the topviewcontroler as childviewcontroller of the rootviewcontroller. After adding the topviewcontroller as childviewcontroller it started to working.

简而言之:只需尝试使用以下方法将按钮superview的视图控制器作为childviewcontroller添加到父视图viewcontroller

So in short: just try to add the view controller of buttons superview as childviewcontroller to the parent views viewcontroller with the following method

func addChildViewController(_ childController: UIViewController)

这篇关于Swift 中的 UIButton 没有注册触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)