iOS / Swift:如何实现backButton的longPressed动作?

我试图为我的应用程序中的每个ViewController实现自定义后退button 。 我希望它有两个行动。 如果button被轻敲 ,它应该正常行为,并上升导航堆栈。 如果button按下的时间更长,它应该转到预定义的ViewController。

我怎么才能做到这一点,只有后退快速?

您可以隐藏您的默认导航返回button,并以这种方式添加一个自定义button:

import UIKit class SViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //hide your default back button navigationController!.setNavigationBarHidden(false, animated:true) //create a new button var myBackButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton myBackButton.addTarget(self, action: "popToRoot:", forControlEvents: UIControlEvents.TouchUpInside) myBackButton.setTitle("Back", forState: UIControlState.Normal) myBackButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal) myBackButton.sizeToFit() //create a LongPressGestureRecognizer var longPressGesture = UILongPressGestureRecognizer(target: self, action: "longPressAction:") //add LongPressGestureRecognizer into button myBackButton.addGestureRecognizer(longPressGesture) var myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton) self.navigationItem.leftBarButtonItem = myCustomBackButtonItem } //this method will call when you tap on button. func popToRoot(sender:UIBarButtonItem){ self.navigationController!.popToRootViewControllerAnimated(true) } //this method will call when you long press on button func longPressAction(gestureRecognizer:UIGestureRecognizer) { //initiate your specific viewController here. println("Long press detected") } }