我想使用按钮更改为不同的视图,但我必须点按两次按钮才能使其动作。
为什么我必须点击两次?
以下是代码:
func tapEmergencyKitsDetected(sender:AnyObject) {
myStack.push(thisNC)
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : UIViewController = storyboard.instantiateViewControllerWithIdentifier("NCEmergencyKitsIntro") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
func tapEmergencyPlanDetected(sender:AnyObject) {
myStack.push(thisNC)
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc : UIViewController = storyboard.instantiateViewControllerWithIdentifier("NCEmergencyPlanList") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
@IBAction func tapEmergencyKits(sender: UIButton) {
self.buttonEmergencyKits.addTarget(self, action: Selector("tapEmergencyKitsDetected:"), forControlEvents: UIControlEvents.TouchDown)
}
@IBAction func tapEmergencyPlan(sender: UIButton) {
self.buttonEmergencyKits.addTarget(self, action: Selector("tapEmergencyPlanDetected:"), forControlEvents: UIControlEvents.TouchDown)
}
答案 0 :(得分:0)
按钮确实响应第一次点击。它正是你告诉它做的。但那件事并没有任何可见结果。当你说:
@IBAction func tapEmergencyKits(sender: UIButton) {
self.buttonEmergencyKits.addTarget(
self, action: Selector("tapEmergencyKitsDetected:"),
forControlEvents: UIControlEvents.TouchDown)
}
您所做的就是重新配置按钮self.buttonEmergencyKits
将在下一步点击时执行的操作。事实上,在它的下一次拍打中,它就是它。