检查控制器是否已加载XCode

时间:2011-05-23 11:53:17

标签: objective-c xcode uiviewcontroller uisplitviewcontroller

我有以下代码在Split View Controller中推送一个新的ViewController:

Level4ViewController *controller = [[Level4ViewController alloc] initWithNibName:@"ModuleViewController" bundle:nil];
    [[detailViewController navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil;

我遇到的唯一问题是,如果我再次运行它,将会显示一个新的控制器,我希望能够使用我的所有数据进入之前的视图。

任何人都可以帮助我。

感谢。

编辑:

更新了代码?

Level4ViewController *controller;
    for(UIView *view in self.navigationController.viewControllers)
    {
        if([view isKindOfClass:[Level4ViewController class]])
        {
            controller = view;
            if(controller == nil)
            {
                controller = [[Level4ViewController alloc] initWithNibName:@"ModuleViewController" bundle:nil];
            }
            else {
                controller = [self.navigationController.viewControllers objectAtIndex:1];
            }

        }
    }

    [[detailViewController navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil;

2 个答案:

答案 0 :(得分:1)

UINavigationController有一个属性viewControllers,它是一个NSArray,用于保存已推送到导航控制器的所有堆栈,在此数组中,您可以检查视图控制器是否为是否使用那个 - 你这样检查 -

Level4ViewController *lvc;

for(UIView *view in self.navigationController.viewControllers)
{
     if([view isKindOfClass:[Level4ViewController class]])
     {
          lvc = view;
     }
}

如果您已经知道viewcontroller在哪个索引处,那么您可以从该索引获取它 -

Level4ViewController *lvc = [self.navigationController.viewControllers objectAtIndex:1];

更新 -

Level4ViewController *controller;
for(UIView *view in self.navigationController.viewControllers)
{
     if([view isKindOfClass:[Level4ViewController class]])
     {
         controller = view;
     }
}

if(controller == nil)
{
    controller = [[Level4ViewController alloc] initWithNibName:@"ModuleViewController" bundle:nil];
}

[[detailViewController navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;

答案 1 :(得分:0)

如果您使用的是导航控制器

FirstScreenViewController *firstScreenVC = [self.storyboard instantiateViewControllerWithIdentifier:@"1S"];

    if (![self.navigationController.topViewController isKindOfClass:FirstScreenViewController.class])
        [self.navigationController pushViewController:firstScreenVC animated:YES];