开发者

Cocoa-Touch: Check if UIViewController exists before init it

Is there a way to check if a UIViewController is loaded in memory/visible on screen?

Something like this:

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

That should happen when a button (t开发者_如何学运维hat right now initializes the ViewControllers) is tapped. Right now, when tapped it opens n ViewControllers, it is supposed to display only one at a time.

Thats pretty much it, greetings and hope you can help me out.


Is this based on existing code? Classes should start upper case, and instances should be camel case, e.g.

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

it is probably worth declaring it in the header, i.e.

@interface SomeClass : NSObject {

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

and then in code you could do

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

Also, don't do the else{[contentRv release];} bit, if you have autoreleased it anywhere, this will leak at some point.


If you are using UINavigationController check for the property topViewController.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜