如何推送viewcontroller(视图控制器)?

Mad*_*dav 9 iphone cocoa-touch memory-management objective-c uiviewcontroller

内存管理是iPhone中非常重要的问题.所以我问一个非常普遍的问题.有两种方法可以调用另一个类的viewController.

方式1:

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

[self.navigationController pushViewController:viewController animated:YES];
Run Code Online (Sandbox Code Playgroud)

方式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
Run Code Online (Sandbox Code Playgroud)

所以显而易见的问题是,哪个是调用其他类的viewController的最佳方法?Way1还是Way2?

公开邀请建议和意见.

请评论并投票.

Mor*_*ion 19

嗯......为了简单起见,为什么不呢:

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

[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
Run Code Online (Sandbox Code Playgroud)


Kri*_*son 7

方式1更简单.

方式2允许第一个控制器保持对推动的视图控制器的引用.如果您需要该参考,那么这将是有用的.

这里没有明确的答案.这取决于您的需求.当然,一般规则是使代码尽可能简单,但并不简单.