从UINavigationControllerDelegate中获取当前视图控制器

时间:2011-06-19 15:17:28

标签: iphone objective-c ios4 uiviewcontroller uinavigationcontroller

有没有办法让视图控制器从内部过渡出来:

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

当我使用navigationController.topViewController时,我正在接受我正在进入的那个。 使用navigationController.viewControllers就足够了,以防我在导航系统中越来越深(因为我可以在阵列中寻找前一个控制器),但是如果我移动到“外面”,这将无法工作,可以我需要的视图控制器不再存在。

任何一般的方式吗? 我需要它,所以我可以在动画期间添加一个子视图,当然它已经消失了。

由于

2 个答案:

答案 0 :(得分:0)

我看了一下UINavigationControllerDelegate,我很确定如果你尝试使用委托实现这个,你会遇到很多麻烦才能让这个工作正常,当你可以做更多的事情时通过使用覆盖的UINavigationController的子类来轻松而干净地使用:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
and if you plan to use them 
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
and
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

您还需要将要添加到导航控制器中的过渡视图的视图存储为实例变量。在示例中,我将其称为viewForTransition。

一个被覆盖的方法的示例:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
     // Submit an animation which adds the viewForTransitioning to the view about to be pushed off screen.
     // Upon completion, the animation will remove the viewForTransitioning from it's superview.
     // You also need to figure out the proper duration and delay and store them in constants or as instance variables.

     [UIView animateWithDuration:theDuration delay:theDelay options:UIViewAnimationOptionCurveLinear animations:^{
          [self.topViewController.view addSubview:self.viewForTransition];
     } completion:^{
          [self.viewForTransition removeFromSuperview];
     }];
     // after submitting the animation, call super.
     // if this makes the view appear for a few moments and then disappear before animating to the next view controller, you might try calling super after calling addSubview.
     // if that doesn't work you can try storing the old top view controller in an instance variable so that it can be accessed by the delegate when navigationController:willShowViewController:animated: is called.
     [super pushViewController:viewController animated:animated];
}

我不完全确定这会做你想要的。但值得一试。

答案 1 :(得分:0)

如果你想从导航控制器数组中找到一个特定的ViewController,那么试试这个.........

for(UIViewController * VC in [self.navigationController viewControllers])
{
    if([VC isKindOfClass:[ViewController class]])
    {
        //YOUR CODE ....... 
    }
}

享受编码.........