简体   繁体   中英

How to push viewcontroller ( view controller )?

Memory management is a very important issue in iPhone. So I am asking a very general question. There are two ways to call a the viewController of another class.

Way 1:

AnotherClassViewController *viewController = [[[AnotherClassViewController alloc] initWithNibName:@"AnotherClassView" bundle:nil] autorelease];

[self.navigationController pushViewController:viewController animated:YES];

Way 2:

    #import "AnotherClassViewController.h"

    @interface ThisClassViewController : UIViewController{

      AnotherClassViewController *myViewController;

    }

    @property (nonatomic, retain) AnotherClassViewController *myViewController;

    @end

    @implementation ThisClassViewController

    @synthesize myViewController;

    - (void) pushAnotherViewController{

    if(self.myViewController == nil){

    AnotherClassViewController *tempViewController = [[AnotherClassViewController alloc] initWithNibName:@"AnotherClassView" bundle:nil];

    self.myViewController = tempViewController;

    [tempViewController release];
    }
    [self.navigationController pushViewController:myViewController animated:YES];
    }

- (void)dealloc{
self.myViewController = nil;
}
@end

So the obvious question is, which is the best way to call the viewController of other class ? Way1 or Way2?

Suggestions and comments are openly invited.

Please comment and vote.

Hmm... To keep things simple, why not just:

MyViewController* viewController = [[MyViewController alloc] init];

[self.navigationController pushViewController:viewController animated:YES];
[viewController release];

Way 1 is simpler.

Way 2 lets the first controller keep a reference to the pushed view controller. If you need that reference, then this would be useful.

There is no clear answer here. It depends upon your needs. The general rule, of course, is to make the code as simple as possible, but no simpler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM