Determine if view that appears was pushed or came from back button in navigation bar

前端 未结 3 958
天命终不由人
天命终不由人 2021-02-20 05:55

Is there a way to tell if a new controller came from a navigation back button or was pushed onto the stack? Id like to reload data only for pushing on the navigation stack, not

相关标签:
3条回答
  • 2021-02-20 06:28

    You could implement the UINavigationControllerDelegate and override the `navigationController:didShowViewController:animated:' method. You'll then have to check the returned view controller to make a determination as to whether you came back from the expected view controller.

    - (void)navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
    {
        if (yourPushedViewController == viewController)
        {
            // Do something
        }
    }
    
    0 讨论(0)
  • 2021-02-20 06:31

    If your push also includes instantiating the view controller, put your push-only logic in viewDidLoad. It will not be called on back because it has already been loaded.

    0 讨论(0)
  • 2021-02-20 06:33

    As of iOS 5.0 you can do this:

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        if (self.isBeingPresented || self.isMovingToParentViewController) {
            // "self" is being shown for the 1st time, not because of a "back" button.
        }
    }
    
    0 讨论(0)
提交回复
热议问题