按下后退按钮时如何实际捕获?

时间:2015-12-11 12:14:34

标签: ios swift segue

答案很多,但他们没有回答这个问题。例如,我将以下代码放在:

    override func viewWillDisappear(animated: Bool) {
    if (self.navigationController!.viewControllers.indexOf(self)==nil) {
        print("back button pressed\n")
    }
    super.viewWillDisappear(animated)
}

但即使没有按下后退按钮,“按下后退按钮”也会打印到控制台。场景具有使用展开segue返回到前一场景的按钮,这些按钮会打印“后退按钮”。我只有在按下后退按钮时才需要执行代码。

编辑Muneeba:

import UIKit

class NewViewController: UIViewController, UINavigationBarDelegate {


func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem) {
    performSegueWithIdentifier("returnToStepOne", sender: self)
    delegate.backFromNewViewController()
}
override func viewDidLoad() {
    super.viewDidLoad()
    // self.navigationController.navigationBar.delegate = self;
    navigationController?.navigationBar.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

1 个答案:

答案 0 :(得分:1)

尝试关注UINavigationBarDelegate

 func navigationBar(navigationBar: UINavigationBar, didPopItem item: UINavigationItem)