使用 popToViewController:animated 选择视图控制器的更好方法:
考虑:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,这是假设堆栈只有一个您正在寻找的控制器类的实例。如果还有更多,我认为您可以选择将其存储在全局可访问的位置,例如应用程序委托。
注意 - 通常如果您在 UIViewController 中使用它,最后一行代码将是:
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:
您可以将问题中的代码替换为 UINavigationController 上的简单类别,该类别可以在任何合适的地方使用。
You can replace code in the question with a simple category on
UINavigationController
which can be used wherever it's appropriate.只需与
isKindOfClass
一起使用Simply using with
isKindOfClass