仅在特定视图未显示时弹出viewController

时间:2012-10-22 09:53:50

标签: objective-c ios uiviewcontroller

我有一个viewController,当我移动到应用程序中的另一个选项卡时,我希望他弹出。我的问题是,当我进入这个视图时,我有一个“加号”按钮,用于添加ABPeoplePickerNavigationController的人,比人们选择器变为活动状态时,视图也会弹出,所以当我选择人时,应用程序会崩溃,因为它没有任何回归的观点。

这是viewWillDisappear:

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController popViewControllerAnimated:YES];
}

我该如何解决?

新代码:

- (void)viewWillDisappear:(BOOL)animated
{
    NSArray *controllers =  self.darioTabController.childViewControllers;
    UIView *v;

    for (UIViewController *vc in controllers)
    {
        if ([vc isKindOfClass:[@"ModalPresnterViewController" class]]) // or even [ABPeoplePickerNavigationController class]
        {
            v = vc.view;
        }
        else
            [self.navigationController popViewControllerAnimated:YES];
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望仅在点击标签栏时才能执行操作,则可以使用TabBarController委托方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
  //The user click on the item corresponding to ABPeoplePickerNavigationController
  if([viewController isKindOfClass:[ABPeoplePickerNavigationController class]])
  {
    //Do something if you cant
  }
  else // The user click on an other view controller (pop the ABPeoplePickerNavCtrl)
  {
    [yourController.navigationController popViewControllerAnimated:YES];
  }
}

或者,您可以检查选择了哪个TabBarItem:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
  //Check if the selected view controller (ABPeopleNavCtrl)
  if(tabBarController.selectedIndex == indexOfTheABPeoplePickNavigationController)
  {

  }
  else // Else the user tap on an other item, so pop the controller you want
  {
    [theViewControllerToPop.navigationController popViewControllerAnimated:YES];
  }
}

注意:记住设置UITabBarController委托;)