ios我怎么知道控制器返回“推”或“弹出”

时间:2012-11-05 05:19:23

标签: ios pushviewcontroller viewdidappear popviewcontroller

我有三个控制器,我想知道控制器是推或弹

控制器:

{
    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];
}

感谢。

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

编辑

Add UINavigationControllerDelegate in .h file

也这样做:

[self.yournavController setDelegate:self];

以下方法为navigation controller delegate,当导航控制器shows通过top view controllerpushpop获得新setting时,会调用此方法view controller stack

添加此方法

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

}

答案 2 :(得分:0)

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

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

  2. 当你从A到B,即它是一个推动,使它相等"是"在A。

  3. yourAppDelegate *myDelegate = (yourAppDelegate*) [[UIApplication SharedApplication] delegate];
    
    myDelegate.isPush = YES;
    

    同样在从C弹出之前,设为isPush = NO;

    的值
    1. 在B' viewDidLoad中,查看变量的值。
    2. yourAppDelegate *myDelegate = (yourAppDelegate*) [[UIApplication SharedApplication] delegate];
      
      if(myDelegate.isPush)
      //means A was pushed
      
      else
      //means C was popped