更好的方式来选择视图控制器使用popToViewController:animated: [英] Better way to select view controller using popToViewController: animated:

查看:138
本文介绍了更好的方式来选择视图控制器使用popToViewController:animated:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:1] animated:YES];

有更好的方法获取视图控制器的索引来弹出吗?这样如果我做一些事情到nav栈,我不必回去编辑这个。我正在想可能将它存储在VC上的一个ivar或使用#define宏。任何想法?

Is there a better way to get index of the view controller to pop to? This way if I do something to the nav stack, I don't have to go back and edit this. I was thinking of maybe storing it in a ivar on the VC or using #define macros. Any ideas?

编辑:
堆栈有四个视图控制器。我使用这个代码从第四个到第二个。

The stack has four view controllers. I used this code to pop from the fourth to the second.

推荐答案

UINavigationController 可以在任何适当的地方使用。

You can replace code in the question with a simple category on UINavigationController which can be used wherever it's appropriate.

@interface UINavigationController(indexPoping)
- (void)popToViewControllerAtIndex:(NSInteger)newVCsIndex animated:(BOOL)animated;
@end

@implementation UINavigationController(indexPoping)
- (void)popToViewControllerAtIndex:(NSInteger)newVCsIndex animated:(BOOL)useAnimation
{
    if (newVCsIndex < [self.viewControllers count]) {
        [self popToViewController:[self.viewControllers objectAtIndex:newVCsIndex] animated:useAnimation];
    }
}


// Usage

...

NSInteger indexToPopTo = ...
[self.navigationController popToViewControllerAtIndex:indexToPopTo animated:YES]

...

这篇关于更好的方式来选择视图控制器使用popToViewController:animated:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆