检查 UIViewController 是否已经创建 [英] Check to see if UIViewController has already been created

查看:32
本文介绍了检查 UIViewController 是否已经创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to check if a viewcontroller has already been created. If the view already exists then it should become the current view. If it doesn't exist then it should be created and made visible.

我的代码是

            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 循环.任何帮助都会很棒!

I'm not sure where I'm going wrong but it would appear that the for loop isn't being executed. Any help would be great!

推荐答案

试试这个:

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];

这篇关于检查 UIViewController 是否已经创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆