我遇到一段代码要弹出到导航堆栈中的特定viewcontroller,如下所示
for (UIViewController* viewController in self.navigationController.viewControllers) {
if ([viewController isKindOfClass:[MyGroupViewController class]] ) {
MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;
[self.navigationController popToViewController:groupViewController animated:YES];
}
}
目标是弹出MyGroupViewController。但我不理解这行代码。
MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;
这里到底发生了什么。我不认为在这里创建了一个新的MyGroupViewController实例。
答案 0 :(得分:21)
//This for loop iterates through all the view controllers in navigation stack.
for (UIViewController* viewController in self.navigationController.viewControllers) {
//This if condition checks whether the viewController's class is MyGroupViewController
// if true that means its the MyGroupViewController (which has been pushed at some point)
if ([viewController isKindOfClass:[MyGroupViewController class]] ) {
// Here viewController is a reference of UIViewController base class of MyGroupViewController
// but viewController holds MyGroupViewController object so we can type cast it here
MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;
[self.navigationController popToViewController:groupViewController animated:YES];
}
}
你也可以这样做
if ([viewController isKindOfClass:[MyGroupViewController class]] ) {
[self.navigationController popToViewController:viewController animated:YES];
}
Swift代码
//Itrate through all the view controllers in navigation stack
for vc in self.navigationController!.viewControllers {
// Check if the view controller is of MyGroupViewController type
if let myGropupVC = vc as? MyGroupViewController {
self.navigationController?.popToViewController(myGropupVC, animated: true)
}
}
答案 1 :(得分:2)
正在枚举导航控制器堆栈的视图控制器。由于这些视图控制器可以是任何类型(但始终从UIViewController继承),因此使用通用的UIViewController。但是,编译器将不知道该视图控制器是什么类型,因此它被转换为MyGroupViewController。当发生这种情况时,编译器会知道类的类型,并且可以向它发送仅适用于该类的消息。
在这种情况下,它有点不必要,因为它可以简化为:
(UIViewController* viewController in self.navigationController.viewControllers) {
if ([viewController isKindOfClass:[MyGroupViewController class]] ) {
[self.navigationController popToViewController:viewController animated:YES];
}
}
简答:它将变量类型更改为括号中指定的类型,以避免编译器警告。
答案 2 :(得分:1)
for (int m=0; m<[self.navigationController.viewControllers count]; m++)
{
if([[self.navigationController.viewControllers objectAtIndex:m] isKindOfClass:[MyGroupViewController class]])
{
MyGroupViewController * groupViewController = [self.navigationController.viewControllers objectAtIndex:m];
[self.navigationController popToViewController:groupViewController animated:YES];
}
}
答案 3 :(得分:0)
- (void) RetunToSpecificViewController{
for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[AnOldViewController class]]) {
//Do not forget to import AnOldViewController.h
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
On Swift
func RetunToSpecificViewController()
{
let viewControllers: [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
self.navigationController!.popToViewController(viewControllers[viewControllers.count
- 5], animated: true)
}
我们已经写了一篇更好的教程,你可以检查一下 https://appengineer.in/2014/03/13/pop-to-specific-view-controller-in-ios/