Question

i have three controller and i wanna kown the controller is a push or pop

A controller:

{
    if(!b)
     b = [B alloc] init];
    [self.navigationController pushViewController:b animated:YES];
}

B controller:

- (void) viewDidAppear:(BOOL)animated 
{
     [super viewDidAppear:animated];
     //I want here to judge, from the "A" push over, or to return from the "C" "pop"

     //if it is push from A 
     //dosomething.....


     //if it is pop from C
     //dosomething
}
-(void)testAction:(id)sender
{
    C *c = [[C alloc] init];
    [self.navigationController pushViewController:b animated:YES];
    [c release];
}

C controller:

{
    [self.navigationController popViewControllerAnimated:YES];
}

thanks.

Was it helpful?

Solution 2

EDIT :

Add UINavigationControllerDelegate in .h file

Also do this:

[self.yournavController setDelegate:self];

Method below is navigation controller delegate which is called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.

Add this method

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{

}

OTHER TIPS

Have a look at the UIViewController method, isMovingToParentViewController. This will return YES if the view controller is being shown because it was pushed, but NO if it is shown because another view controller was popped off the stack.

-(void)viewDidAppear:(BOOL)animated { //Code in view controller B
    [super viewDidAppear:animated];
    NSLog(@"isMovingToParentViewController: %d",self.isMovingToParentViewController);
    // this will log 1 if pushing from A but 0 if C is popped
}

Hmm I think for that you need to track a global variable that knows either it is push from A or pop from C. What I would do is:

  1. Declare a BOOL variable isPush in appDelegate or some external .h file and synthesize it.

  2. When you are going from A to B i.e. it is a push, make it equal "YES" in A.

yourAppDelegate *myDelegate = (yourAppDelegate*) [[UIApplication SharedApplication] delegate];

myDelegate.isPush = YES;

Similarly before popping from C, make value of isPush = NO;

  1. In B's viewDidLoad, see the value of variable.
yourAppDelegate *myDelegate = (yourAppDelegate*) [[UIApplication SharedApplication] delegate];

if(myDelegate.isPush)
//means A was pushed

else
//means C was popped
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top