Can i know in viewWillAppear that it was called after navigationController pop (back button)?

后端 未结 3 1984
感动是毒
感动是毒 2021-02-06 23:58

Say I have UIViewController A and B. User navigates from A to B with a push segue. Than user presses back button and comes to A.

Now viewWillAppear

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-07 00:41

    I like to do the following in view controller A:

    - (void) viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        if (_popping) {
            _popping = false;
            NSLog(@"BECAUSE OF POPPING");
        } else {
            NSLog(@"APPEARING ANOTHER WAY");
        }
    
        //keep stack size updated
        _stackSize = self.navigationController.viewControllers.count;
    
        ....
    }
    - (void) viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
    
        _popping = self.navigationController.viewControllers.count > _stackSize;
    
        ....
    }
    

    What you are doing is keeping track of whether your view controller (A) is disappearing because a view controller (B) is being pushed or for another reason. Then (if you did not modify the child view controller order) it should accurately tell you if (A) is appearing because of a pop on the navigation controller.

提交回复
热议问题