1

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.

zt9788
  • 948
  • 4
  • 16
  • 31

3 Answers3

7

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
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • @zt9788, I've tested this, and it does work. Did you put the method in the viewDidAppear:animated: method? That's where it belongs. – rdelmar Nov 05 '12 at 07:18
0

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
{

}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • i am sorry. when the controller which C pop to B that navigationController is not worked ,and i use A push B the function of navigationController not work too.why? – zt9788 Nov 05 '12 at 06:03
  • thanks ,i fix it .i have a baseController ,i use self.navigationController.delegate = self; in viewDidLoad it was worked when A push B ,but is is not work when C pop B.---i take -(void)viewDidAppear:(BOOL)animated{self.navigationController.delegate = self;} it is worked. – zt9788 Nov 05 '12 at 06:24
0

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
NightFury
  • 13,436
  • 6
  • 71
  • 120