ios Swift 中的 UIButton 未注册触摸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24037841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton in Swift is not registering touches
提问by Phillip
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:
我正在尝试使用 Swift 创建一个 UIButton。它编译得很好,我可以在模拟器中看到我的按钮,但是当我点击它时,没有任何反应。这是我正在使用的代码:
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)
In the same class, here is the function 'touchedSet':
在同一个类中,这里是函数“touchedSet”:
func touchedSet(sender: UIButton!) {
println("You tapped the button")
}
I'm using the simulator as I don't have an iOS 8.0 device, could that be the problem?
我正在使用模拟器,因为我没有 iOS 8.0 设备,这可能是问题吗?
Thanks!
谢谢!
回答by Mert
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.
我有两个视图控制器。一个视图覆盖其他视图,按钮位于顶视图。
eg.
例如。
Rootviewcontroller has back view
Topviewcontroller has top view
Rootviewcontroller 有后视图
Topviewcontroller 有顶视图
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.
如果我不将 topviewcontroler 添加为 rootviewcontroller 的 childviewcontroller,则顶视图上的按钮不会调用该操作。添加 topviewcontroller 作为 childviewcontroller 后,它开始工作。
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)
回答by Fogmeister
Selectors are a struct that have to be created.
选择器是一个必须创建的结构。
Do it this way...
这样做...
Updated for Swift 4
为 Swift 4 更新
settingsButton.addTarget(self, action: #selector(showSettings), for: .touchUpInside)
That should do it.
那应该这样做。
回答by Fogmeister
In the simulator, sometimes it doesn't recognise the selector. There is a bug it seems. I just changed the action name (selector), and it worked.
在模拟器中,有时它无法识别选择器。似乎有一个错误。我刚刚更改了操作名称(选择器),它起作用了。
let buttonPuzzle:UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
buttonPuzzle.backgroundColor = UIColor.greenColor()
buttonPuzzle.setTitle("Puzzle", forState: UIControlState.Normal)
buttonPuzzle.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
buttonPuzzle.tag = 22;
self.view.addSubview(buttonPuzzle)
Selector Function is Here:
选择器功能在这里:
func buttonAction(sender:UIButton!)
{
var btnsendtag:UIButton = sender
if btnsendtag.tag == 22 {
//println("Button tapped tag 22")
}
}
回答by ouven
For those who are also stuck in the tutorial:
对于那些也被困在教程中的人:
I ran in to the same problem, when I was following Apple's "Start Developing iOS Apps (Swift)". It turned out that I had overseen these code lines in the tutorial
我在关注 Apple 的“开始开发 iOS 应用程序(Swift)”时遇到了同样的问题。原来我已经在教程中监督了这些代码行
override var intrinsicContentSize : CGSize {
return CGSize(width: 240, height: 44)
}
Adding them to my RatingControl fixed the problem.
将它们添加到我的 RatingControl 解决了这个问题。
回答by Lord Fresh
I had a similar problem when i had a subViewController with buttons and tapHandlers to match, and I wanted to place this to a stack in a separate superViewController..
当我有一个带有按钮和 tapHandlers 匹配的 subViewController 时,我遇到了类似的问题,我想将它放在一个单独的 superViewController 中的堆栈中。
This cause none of the tapHandlers to to trigger, and the solution was to instead of only using addArrangedSubview(subViewController.view), I also used addChildViewController(subViewController)
in the superview to allow the childViewController to continue to operate as a viewController and not just a view.
这导致没有任何 tapHandlers 触发,解决方案是,而不是仅使用 addArrangedSubview(subViewController.view),我还在 superview 中使用addChildViewController(subViewController)
,以允许 childViewController 继续作为 viewController 运行,而不仅仅是视图。
回答by Bohdan Savych
I have had this problem when parent view of my button has isUserInteractionEnabled == false
. All subviews will have the same userInteraction
as their parent view. So I have just added this line parentView.isUserInteractionEnabled = true
and the problem disappeared. Hope this helps someone.
当我的按钮的父视图具有isUserInteractionEnabled == false
. 所有子视图都将与userInteraction
其父视图相同。所以我刚刚添加了这一行 parentView.isUserInteractionEnabled = true
,问题就消失了。希望这可以帮助某人。
回答by simotunes
I had the same issue when implementing UIButton and SearchBar on container view(UIView). In my case, the cause was the constraint issue. Constraint to searchBar had issue and because of this, function set had never been called.
在容器视图(UIView)上实现 UIButton 和 SearchBar 时,我遇到了同样的问题。就我而言,原因是约束问题。对 searchBar 的约束有问题,因此,从未调用过函数集。
You can see if there's any from "Debug View Hierarchy" button. (constraint problem is shown as purple warning)
您可以从“调试视图层次结构”按钮中查看是否有任何内容。(约束问题显示为紫色警告)
(env: iOS12, xcode10.1)
(环境:iOS12,xcode10.1)
回答by BonanzaDriver
Interestingly enough, I just ran into the same issue on the very latest versions of iOS/Xcode (v12.4 / v10.3). Turns out the issue for me was a LayoutConstraint! No joke. I had a leading label with the uiSwitch to the right, and found that I needed to change the constraints between the two such that it wasn't a fixed constant value of 8 points (Label-8-Switch-0-|).
有趣的是,我刚刚在最新版本的 iOS/Xcode (v12.4 / v10.3) 上遇到了同样的问题。原来我的问题是 LayoutConstraint!不是开玩笑。我有一个带有右侧 uiSwitch 的前导标签,发现我需要更改两者之间的约束,使其不是 8 点的固定常量值 (Label-8-Switch-0-|)。
As soon as I changed this to a ">=" the Switch was able to change states between on/off. Laughably, it's almost like it wasn't able to change because it needs "room" (that apparently varies) to make the change with.
一旦我将其更改为 ">=" 开关就能够在开/关之间改变状态。可笑的是,它几乎无法改变,因为它需要“空间”(显然会有所不同)来进行改变。
Not sure, but file it away as one of those "hummmm?" items in your memory.
不确定,但将其归档为其中之一“嗯?” 记忆中的物品。
One other item (that really shouldn't matter, frankly) is the valueChanged and tappedUpInside were both not firing (true on both an actual handset and on the simulators). Also, they were hooked up through a storyboard. But, this shouldn't matter as far as I know.
另一项(坦率地说,这真的不重要)是 valueChanged 和 tappedUpInside 都没有触发(在实际手机和模拟器上都是如此)。此外,他们通过故事板联系在一起。但是,据我所知,这应该无关紧要。
回答by Samrez Ikram
So you should use following line
所以你应该使用以下行
settings.userInteractionEnabled = true
回答by Carlos Lago González
The problem with that tutorial from Apple is the way they put the UIView (RatingControl) inside the StackView, which is incorrect. The UIView (RatingControl) should be outside of the StackView. So, the solution is: 1. Drag the UIView (RatingControl) outside of the StackView 2. Set the constraints for the new position of the RatingControl - Leading margin to the Container equal zero - Trailing margin to the Container equal zero - Top margin to the StackView equal zero
Apple 的那个教程的问题在于他们将 UIView (RatingControl) 放在 StackView 中的方式,这是不正确的。UIView (RatingControl) 应该在 StackView 之外。因此,解决方案是: 1. 将 UIView (RatingControl) 拖到 StackView 之外 2. 设置RatingControl 新位置的约束 - Container 的前导边距等于零 - Container 的尾距边距等于零 - Top margin to StackView 等于零
With this new constraints, now the RatingControl will have the same with as the Container, no matter what is the device screen size, and always will be just behind of the StackView where the photo selector will appear.
有了这个新的约束,现在 RatingControl 将与容器相同,无论设备屏幕大小如何,并且始终位于照片选择器将出现的 StackView 的后面。
I hope this help you guys.
我希望这对你们有帮助。