使用 popToViewController:animated 选择视图控制器的更好方法:

发布于 2024-11-08 21:09:08 字数 326 浏览 0 评论 0原文

考虑:

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

是否有更好的方法来获取要弹出的视图控制器的索引?这样,如果我对导航堆栈执行某些操作,我就不必返回并进行编辑。我正在考虑将其存储在 VC 上的 ivar 中或使用 #define 宏。有什么想法吗?

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

Consider:

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2024-11-15 21:09:08
YourViewController *yourViewController;
for ( UIViewController *viewController in self.navigationController.viewControllers ) {
    if ( [viewController isMemberOfClass:[YourViewController class]] ) {
        yourViewController = (YourViewController*)viewController;
        break;
    }
}

[self popToViewController:yourViewController animated:YES];

当然,这是假设堆栈只有一个您正在寻找的控制器类的实例。如果还有更多,我认为您可以选择将其存储在全局可访问的位置,例如应用程序委托。


注意 - 通常如果您在 UIViewController 中使用它,最后一行代码将是:

[self.navigationController popToViewController:seuleMainPage animated:YES];
YourViewController *yourViewController;
for ( UIViewController *viewController in self.navigationController.viewControllers ) {
    if ( [viewController isMemberOfClass:[YourViewController class]] ) {
        yourViewController = (YourViewController*)viewController;
        break;
    }
}

[self popToViewController:yourViewController animated:YES];

Of course, this assumes the stack will have only one instance of the controller class that you are looking for. If there are more, I think you can choose to store it in a globally accessible location like the application delegate.


Note - typically if you are using this in a UIViewController, that final line of code would be:

[self.navigationController popToViewController:seuleMainPage animated:YES];
何以心动 2024-11-15 21:09:08

您可以将问题中的代码替换为 UINavigationController 上的简单类别,该类别可以在任何合适的地方使用。

@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]

...

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]

...
筱武穆 2024-11-15 21:09:08

只需与 isKindOfClass 一起使用

[self.navigationController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if([obj isKindOfClass:[YourViewController class]]) {
            YourViewController *objVC = obj;
            [self.navigationController popToViewController:objVC animated:YES];
            *stop = YES;
        }
    }];

Simply using with isKindOfClass

[self.navigationController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if([obj isKindOfClass:[YourViewController class]]) {
            YourViewController *objVC = obj;
            [self.navigationController popToViewController:objVC animated:YES];
            *stop = YES;
        }
    }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文