1

我有三个控制器,我想知道控制器是推送还是弹出

控制器:

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

B控制器:

- (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控制器:

{
    [self.navigationController popViewControllerAnimated:YES];
}

谢谢。

4

3 回答 3

7

看一下 UIViewController 方法,isMovingToParentViewController。如果视图控制器因为被推送而被显示,这将返回 YES,但如果因为另一个视图控制器从堆栈中弹出而显示它,则返回 NO。

-(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
}
于 2012-11-05T05:29:57.663 回答
0

编辑

Add UINavigationControllerDelegate in .h file

也这样做:

[self.yournavController setDelegate:self];

下面的方法navigation controller delegate是当导航控制器通过shows一个新的时调用的,或者。top view controllerpushpopsettingview controller stack

添加这个方法

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

}
于 2012-11-05T05:27:38.597 回答
0

嗯,我认为为此您需要跟踪一个全局变量,该变量知道它是从 A 推送还是从 C 弹出。我要做的是:

  1. 在 appDelegate 或一些外部 .h 文件中声明一个BOOL变量并合成它。isPush

  2. 当您从 A 到 B 时,即这是一个推,在 A 中使其等于“是”。

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

myDelegate.isPush = YES;

同样,在从 C 中弹出之前,为isPush = NO;赋值

  1. 在 B'sviewDidLoad中,查看变量的值。
yourAppDelegate *myDelegate = (yourAppDelegate*) [[UIApplication SharedApplication] delegate];

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

else
//means C was popped
于 2012-11-05T05:36:25.147 回答