将视图控制器弹出到公共视图控制器

时间:2014-02-27 10:52:42

标签: ios objective-c uinavigationcontroller

我有一个类似app的仪表板,在某些时候我想回到“Dashboard”视图控制器。我无法更改根视图控制器。我完成此任务的方式是通过静态实例。我使用这段代码:

static DashboardViewController *instance;
+ (instancetype) instance {
  return instance;
}

此实例在viewDidLoad中设置为self。

那么当我想要弹出这个控制器时我会使用:

  [self.navigationController popToViewController:[DashboardViewController instance] animated:YES];

我想知道这是怎么回事,还是应该有所不同?

2 个答案:

答案 0 :(得分:4)

试试这个,

for (UIViewController  *viewController in [self.navigationController viewControllers]) {

    //you can also use NSClassFromString(@"DashboardViewController") instead of [DashboardViewController class]
    if ([viewController isKindOfClass:[DashboardViewController class]]) {
        [self.navigationController popToViewController:viewController animated:YES];
    }
}

答案 1 :(得分:3)

for (UIViewController *controller in self.navigationController.viewControllers)
{
    if ([controller isKindOfClass:[DashboardViewController class]])
    {
        //Do not forget to import DashboardViewController.h

        [self.navigationController popToViewController:controller animated:YES];
        break;
    }
}