视图中的按钮未单击 ios swift
Posted
技术标签:
【中文标题】视图中的按钮未单击 ios swift【英文标题】:Button in view not clicking ios swift 【发布时间】:2015-09-24 14:02:33 【问题描述】:我正在尝试通过在导航栏下方添加一个视图并最初将其设置为隐藏直到按下 navigationBarItem 按钮来在 Swift 中实现一个下拉菜单,这很有效。在下拉视图中,我添加了两个按钮,如下面的代码所示,但它似乎没有接收到事件。
var isAnimating: Bool = false
var dropDownViewIsDisplayed : Bool = false
var dropDownView : UIView!
var buttonOne : UIButton!
var buttonTwo : UIButton!
var screenWidth : CGFloat!
@IBOutlet weak var searchNavigationBar: UINavigationItem!
override func viewDidLoad()
super.viewDidLoad()
screenWidth = self.view.bounds.size.width
self.navigationController?.navigationBar.translucent = false
dropDownView = UIView(frame: CGRectMake(0, -15, screenWidth, -80))
dropDownView.hidden = true
dropDownView.userInteractionEnabled = true
self.navigationController?.view.insertSubview(self.dropDownView, belowSubview: (self.navigationController?.navigationBar)!)
buttonOne = UIButton(frame: CGRectMake(0, 0, screenWidth, 40))
buttonOne.setTitle("Button One", forState: .Normal)
buttonOne.setTitleColor(UIColor.blackColor(), forState: .Normal)
buttonOne.backgroundColor = UIColor.whiteColor()
buttonOne.addTarget(self, action: Selector("buttonOnePressed"), forControlEvents: UIControlEvents.TouchUpInside)
buttonOne.userInteractionEnabled = true
dropDownView.addSubview(buttonOne)
buttonTwo = UIButton(frame: CGRectMake(0, buttonOne.bounds.size.height, screenWidth, 40))
buttonTwo.setTitle("Button Two", forState: .Normal)
buttonTwo.setTitleColor(UIColor.blackColor(), forState: .Normal)
buttonTwo.backgroundColor = UIColor.whiteColor()
buttonTwo.addTarget(self, action: Selector("buttonTwoPressed"), forControlEvents: UIControlEvents.TouchUpInside)
buttonTwo.userInteractionEnabled = true
dropDownView.addSubview(buttonTwo)
func buttonTwoPressed()
self.performSegueWithIdentifier("showLocation", sender: self)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
if (segue.identifier == "showLocation")
var location: LocationTableViewController = (segue.destinationViewController as? LocationTableViewController)!
按钮点击函数没有被调用。
【问题讨论】:
你能显示buttonOnePressed
函数吗?
请确保您在UIButton
之上没有一些透明视图,并且该操作按钮未实现为buttonOnePressed:
@bsarr007 更新以显示功能
按下按钮时是否有视觉反馈?
暂时没有,只使用断点和调试器,但没有调用函数。
【参考方案1】:
您是否将 Swift 2.0 与 Xcode 7 一起使用?只是通过快速尝试像这样使用您的代码,我发现事件处理程序被正确调用而没有任何大的变化。你确定其他地方没有问题吗?
var isAnimating: Bool = false
var dropDownViewIsDisplayed : Bool = false
var dropDownView : UIView!
var buttonOne : UIButton!
var buttonTwo : UIButton!
var screenWidth : CGFloat!
@IBOutlet weak var searchNavigationBar: UINavigationItem!
override func viewDidLoad()
super.viewDidLoad()
screenWidth = self.view.bounds.size.width
self.navigationController?.navigationBar.translucent = false
// I modified these 2 lines to test your code immediately
dropDownView = UIView(frame: CGRectMake(0, 65, screenWidth, 80))
dropDownView.hidden = false
dropDownView.userInteractionEnabled = true
self.navigationController?.view.insertSubview(self.dropDownView, belowSubview: (self.navigationController?.navigationBar)!)
buttonOne = UIButton(frame: CGRectMake(0, 0, screenWidth, 40))
buttonOne.setTitle("Button One", forState: .Normal)
buttonOne.setTitleColor(UIColor.blackColor(), forState: .Normal)
buttonOne.backgroundColor = UIColor.whiteColor()
buttonOne.addTarget(self, action: Selector("buttonOnePressed"), forControlEvents: UIControlEvents.TouchUpInside)
buttonOne.userInteractionEnabled = true
dropDownView.addSubview(buttonOne)
buttonTwo = UIButton(frame: CGRectMake(0, buttonOne.bounds.size.height, screenWidth, 40))
buttonTwo.setTitle("Button Two", forState: .Normal)
buttonTwo.setTitleColor(UIColor.blackColor(), forState: .Normal)
buttonTwo.backgroundColor = UIColor.whiteColor()
// Here I just wanted to show you that calling Selector() is not necessary at all
buttonTwo.addTarget(self, action: "buttonTwoPressed", forControlEvents: UIControlEvents.TouchUpInside)
buttonTwo.userInteractionEnabled = true
dropDownView.addSubview(buttonTwo)
// I didn't see this method in your code above so I added to test and it works!
func buttonOnePressed()
print("buttonOnePressed")
// This is also being called normally
func buttonTwoPressed()
print("buttonTwoPressed")
【讨论】:
很奇怪,它不适用于我的。也许是因为控制器是主故事板中导航控制器的根控制器? 我的视图控制器也是导航控制器的根视图控制器,所以它一定不是重点。你能通过 GitHub 分享你的代码吗? 嘿,谢谢,我现在已经解决了。与它设置的初始帧有关。感谢您的指点【参考方案2】:你可以试试:
buttonOne.addTarget(self, action: Selector("buttonOnePressed:"), forControlEvents: UIControlEvents.TouchUpInside)
buttonTwo.addTarget(self, action: Selector("buttonTwoPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
【讨论】:
在您分享了您的按钮处理程序代码后,我看到您已正确实现它。你检查是否有一些透明视图覆盖按钮? 你如何检查?这个控制器是故事板中导航控制器的根视图控制器。【参考方案3】:它不起作用,因为它在self.navigationController?.navigationBar.frame
之外。您应该在self.view
或透明模态视图/UIWindow 上添加按钮。
【讨论】:
以上是关于视图中的按钮未单击 ios swift的主要内容,如果未能解决你的问题,请参考以下文章
通过单击 Swift 3 中的 Alert Ok 按钮,翻转回同一视图控制器上的另一个视图
单击一个按钮控件后需要转到同一个视图控制器中的另一个按钮,但导致错误