2011-09-16 42 views
0

所以我在我的didFinishLaunchingWithOptions中有以下代碼。問題是,如果我去掉下面的addSubView和makeKeyVisible MainViewController的viewDidLoad被調用兩次:應用程序委託和viewDidLoad調用兩次

MainViewController * tabBarController = [[MainViewController alloc] init]; 
    navigationController.delegate = self; 
    //[self.window addSubview:navigationController.view]; 
    //[self.window makeKeyAndVisible]; 
    [tabBarController release]; 

    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge| 
    UIRemoteNotificationTypeAlert| 
    UIRemoteNotificationTypeSound]; 

    return YES; 

誰能告訴我,爲什麼,以及如何防止這種情況?對我來說,當你實際分配一個UIViewController時,它調用viewDidLoad並不合理。

回答

1

它確實有道理。來自UIViewController頭文件:

@property(nonatomic,retain) UIView *view; // The getter first invokes [self loadView] if the view hasn't been set yet. Subclasses must call super if they override the setter or getter. 

- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.