app delegate和viewDidLoad两次调用

时间:2011-09-16 02:41:17

标签: objective-c uiviewcontroller

所以我在didFinishLaunchingWithOptions中有以下代码。问题是如果我取消注释下面的addSubView和makeKeyVisible,则调用MainViewController的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 个答案:

答案 0 :(得分: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.