[Objective-C]应用程序委托和 viewDidLoad 调用两次

发布时间: 2016/9/22 4:20:57
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以我有下面的代码在我的 didFinishLaunchingWithOptions。问题是 MainViewController 的 viewDidLoad 被称为两次如果我取消注释的 addSubView 和 makeKeyVisible 下面︰

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

    [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|

    return YES;

谁能告诉我为什么,以及如何防止这种情况吗?对我来说不很有道理,当你实际上分配它调用 viewDidLoad UIViewController。

解决方法 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.