Ios 视图中的按钮未单击

Ios 视图中的按钮未单击,ios,swift,Ios,Swift,我试图在Swift中实现一个下拉菜单,在导航栏下面添加一个视图,并在按下navigationBarItem按钮之前将其初始设置为hidden,这样就可以了。在下拉视图中,我添加了两个按钮,如下面的代码所示,但它似乎没有拾取事件 var isAnimating: Bool = false var dropDownViewIsDisplayed : Bool = false var dropDownView : UIView! var buttonOne : UIButton! var butto

我试图在Swift中实现一个下拉菜单,在导航栏下面添加一个视图,并在按下navigationBarItem按钮之前将其初始设置为hidden,这样就可以了。在下拉视图中,我添加了两个按钮,如下面的代码所示,但它似乎没有拾取事件

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)!

    }
}

未调用按钮单击功能。

请尝试以下功能:

buttonOne.addTarget(self, action: Selector("buttonOnePressed:"), forControlEvents: UIControlEvents.TouchUpInside)

buttonTwo.addTarget(self, action: Selector("buttonTwoPressed:"), forControlEvents: UIControlEvents.TouchUpInside)

您是否将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")
}

它无法工作,因为它位于self.navigationController?.navigationBar.frame之外。您应该在
self.view
或透明模式视图/ui窗口上添加按钮。

您可以显示
按钮未按下的功能吗?请确保
ui按钮上方没有透明视图,并且操作按钮未实现为
按钮未按下:
@bsar007更新为显示功能您有吗当您按下按钮时会出现视觉反馈?目前没有,只使用断点和调试器,但不会调用函数。在您共享按钮处理程序代码后,我看到您已正确实现了它。你有没有检查是否有一些透明的视图覆盖按钮?你如何检查?此控制器是故事板中导航控制器的根视图控制器。非常奇怪,它在我的上不起作用。可能是因为控制器是主故事板中导航控制器的根控制器?我的视图控制器也是导航控制器的根视图控制器,因此它不能是重点。你能通过GitHub分享你的代码吗?嘿,谢谢,我现在已经整理好了。与设置的初始帧有关。谢谢你的指点