0

我正在尝试检查是否已经创建了视图控制器。如果视图已经存在,那么它应该成为当前视图。如果它不存在,那么应该创建它并使其可见。

我的代码是

            DemoViewController *demoController = [DemoViewController alloc];
        for(DemoViewController *view in self.navigationController.viewControllers)
        {
            if([view isKindOfClass:[DemoViewController class]])
            {
                viewExists=true;
                demoController=view;
            }

        }
        if (!viewExists) {
            demoController initWithNibName:@"DemoViewController" bundle:nil;
        }
        [view release];
        [demoController release];

我不确定我哪里出错了,但似乎 for 循环没有被执行。任何帮助都会很棒!

4

1 回答 1

0

试试这样:

DemoViewController* demoController = nil;
for(int vv=0; vv<[self.navigationController.viewControllers count]; ++vv) {
    NSObject *vc = [self.navigationController.viewControllers objectAtIndex:vv];
    if([vc isKindOfClass:[DemoViewController class]]) {
        demoController = (DemoViewController*)vc;
    }
}

if (demoController == nil) {
    demoController = [[DemoViewController alloc] initWithNibName:@"DemoViewController" bundle:nil];
    // Do we need to push it into navigation controller?
}

[demoController release];
于 2012-09-17T00:52:54.027 回答