Find out if user pressed the back button in uinavigationcontroller?

后端 未结 7 1638
时光说笑
时光说笑 2020-12-01 03:34

When a view loads, i want to see if it\'s because the user pressed the back button. How can i check this?

7条回答
  •  死守一世寂寞
    2020-12-01 03:46

    This is a slightly different scenario, but I thought the solution might help others out.

    In my situation, I had a UINavigationController within a UIPopoverController. I needed to detect whether the user clicked the back button, or clicked outside of the popover. To do this I checked the visibleViewController property in viewWillDisappear. If the view controller is still the visibleViewController when closing, then the popover is being closed by another means. If the view controller is not the visibleViewController when closing, then the back button was pressed.

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    
        if (self.navigationController.visibleViewController != self) {
            
        } else {
            
        }
    }
    

    I tried using zach's solution, but isMovingFromParentViewController returns true for both cases.

    I verified this works in iOS 5+

    I hope this helps.

提交回复
热议问题