我有两个ViewControllers,第一个和第二个。它们与show(push)segue相关联。我点击firstViewController上的按钮转到secondViewController。然后使用自动添加的导航控制器<First
,我回到firstViewController。但是,这里我想在按下导航控制器到firstViewContoller时收到警告消息。我该怎么做?
答案 0 :(得分:2)
您要找的是UINavigationControllerDelegate
。
我相信为您提供所需信息的方法是
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
并且
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
在您的CustomViewController中,您将希望符合UINavigationControllerDelegate
协议,如下所示:
@interface CustomViewController : UIViewController <UINavigationControllerDelegate>
然后覆盖上面的委托方法以获取您要查找的消息。
以下是Swift中的完整实现:
import UIKit
class ViewController: UIViewController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
println(viewController)
}
}
class FirstViewController: ViewController {
}
class SecondViewController: ViewController {
}
答案 1 :(得分:1)
您可以在第二个视图控制器上使用viewWillDisappear方法,如下所示:
- (void)viewWillDisappear:(BOOL)animated
{
if(self.isMovingFromParentViewController){
NSLog(@"Controller being popped");
}
}
在这种情况下,如果正在弹出控制器,则self。 isMovingFromParentViewController 将为true。
您还可以在viewWillAppear上查看self。 isMovingToParentViewController ,以检查是否正在推送控制器。
同样是自我。 isBeingDismissed 和self。 isBeingPresented 可用并参考控制器何时被呈现/解散(模态)。