My setup is simple, and my issue is not very different from this one. However to better explain it I have posted it here:

NavController -> VC1 -> VC2

VC1 is root view controller of NavController. VC2 is accessible via Push segue from VC1.

I want to detect, within VC1, whether:

It appeared directly as root view controller (via Push) It appeared as a result of VC2 being popped

I read the docs which says following should tell me if later is true.

isMovingToParentViewController == NO 

However that is not the case, and above condition ALWAYS turns out to be TRUE. Which means that, (self.isMovingToParentViewController == NO) is always happening.

Here is my code:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;

    //pushed to stack
    if (self.isMovingToParentViewController == YES)
    {
        //First time
      }
    else
    //popped off
    {
        //via Pop from VC2
    }    
}

Same is the case for viewDidAppear, too.

For a matter of fact check, I put breakpoint at the start, and checked that all of the following are FALSE, in both cases:

([self isMovingFromParentViewController])
([self isMovingToParentViewController])
([self isBeingPresented])
([self isBeingDismissed])

What is happening? Is there anything I goofed up in my storyboard? Please help...

有帮助吗?

解决方案

Unfortunately, isMovingToParentViewController isn't true for the root view controller, so I usually handle this situation with a BOOL,

@implementation ViewController {
    BOOL isFirstAppearance;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isFirstAppearance = YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (isFirstAppearance) {
        NSLog(@"root view controller is moving to parent");
        isFirstAppearance = NO;
    }else{
        NSLog(@"root view controller, not moving to parent");
    }
}

其他提示

A simple solution is by adding a flag on viewWillDisappear setting it to YES if the VC1 has been disappeared. Else the view has never been disappeared so it is the first push (RootViewController of Navigation Controller).

Example Code

BOOL hasDisappeared;

-(void)viewWillAppear:(BOOL)animated
{
    if (hasDisappeared==YES) {
        //VC2 has been popped
    }
    else
    {
        //VC1 is the rootViewController
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    //Pushing to VC2
    hasDisappeared=YES;
}

Since it appears that isMovingToParentViewController is only set when a viewController is being pushed to the navigation stack, and not set for the initial rootViewController, I'd suggest using the following:

 if([self.navigationController.viewControllers containsObject:self])
 {
     // being popped to self here
 }
 else
 {
     // being pushed here
 }

You have to check isMovingFromParentViewController in VC2’s viewWillDissapear and call a delegate method implemented in VC1. I.e. VC2 is being removed from its parent nav controller because of being popped.

an alternative flag way ,

if your viewController doesn't support gesture popping,


     var onceDo = true

     override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if onceDo{
            // ...
            //  VC1 is the rootViewController
            onceDo = false
        }
        else{
            //VC2 has been popped
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top