views:

902

answers:

2

Hi,

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.

+1  A: 

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.

Kristopher Johnson
A: 

Hmm... Why not just

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

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