Cocoa-Touch:在init之前检查UIViewController是否存在

时间:2011-07-19 22:58:41

标签: iphone objective-c ipad

有没有办法检查UIViewController是否在内存/可见屏幕上加载?

这样的事情:

if([ContentRvController exists]){
    contentView *ContentRvController = [[contentView alloc] 
        initWithNibName:@"contentView" bundle:nil]; //ContentView is a custom UIViewController
    ....
    //Code to set the UIViewController
    ....
}
else{
    [ContentRvController release];
}

当点击按钮(现在正在初始化ViewControllers)时,应该会发生这种情况。现在,当点击它打开ViewControllers时,它应该一次只显示一个。

这就是它,问候和希望你可以帮助我。

2 个答案:

答案 0 :(得分:0)

如果您使用UINavigationController检查属性topViewController

答案 1 :(得分:0)

这是基于现有代码吗? 类应该从大写开始,而实例应该是驼峰大小写,例如

if([contentRvController exists]){
    ContentView *contentRvController = [[ContentView alloc] 
    initWithNibName:@"contentView" bundle:nil]; //ContentView is a custom UIViewController
    ....
    //Code to set the UIViewController
    ....
}
else{
    [contentRvController release];
}

可能值得在标题中声明它,即

@interface SomeClass : NSObject {

}
@property(non-atomic, retain) ContentView *contentRvController;
@end

然后在代码中你可以做

if(contentRvController!=nil){
    ContentView *aView=[[[ContentView alloc] init] autorelease];
    self.contentRvController=aView;
}

另外,不要执行else {[contentRv release];}位,如果你在任何地方自动释放它,这会在某些时候泄漏。

相关问题