简体   繁体   English

Xcode 4 - viewDidLoad问题

[英]Xcode 4 - viewDidLoad issue

Is anyone else having issues with Xcode 4 where viewDidLoad is being called twice? 是否有其他人在Xcode 4中遇到问题,其中viewDidLoad被调用两次? I have run the same project in both Xcode 3.2 and Xcode 4 and it only acts up in Xcode 4. 我在Xcode 3.2和Xcode 4中运行了相同的项目,它只在Xcode 4中运行。

After researching this on the Apple Developer forums , it seems that in some cases Xcode 4 creates bugged Interface Builder NIBs. Apple Developer论坛上研究之后,似乎在某些情况下Xcode 4会创建有问题的Interface Builder NIB。 The effect is that the application's rootViewController gets loaded in twice, which really screws things up. 结果是应用程序的rootViewController被加载了两次,这真的搞砸了。 The same project loaded in Xcode 3 won't exhibit the problem. 在Xcode 3中加载的相同项目不会出现问题。

In my universal app, it only affected the iPad NIB. 在我的通用应用程序中,它只影响了iPad NIB。 The iPhone was fine. iPhone很好。

I was able to solve this issue by: 我能够通过以下方式解决这个问题:

  • Removing the rootViewController connection in Interface Builder (this causes the app to load with window.rootViewController = nil ) 在Interface Builder中删除rootViewController连接(这会导致应用程序加载window.rootViewController = nil
  • In viewDidLoad for the main controller (the one that was being loaded twice), I then manually assign appDelegate.window.rootViewController = self 在主控制器的viewDidLoad(被加载两次的那个)中,然后我手动分配appDelegate.window.rootViewController = self

So far this seems to have the desired effect. 到目前为止,这似乎具有预期的效果。

I had same issue. 我有同样的问题。 And i resolved it. 我解决了它。 It happens when your app memory did get memory warning. 它发生在您的应用内存确实收到内存警告时。

Put a breakpoint to memoryDidReceiveWarning. 给memoryDidReceiveWarning设一个断点。 It get's called, and clear your class object memory. 它被调用,并清除你的类对象内存。 So your viewDidLoad Get called twice, because it has no memory at that time. 所以你的viewDidLoad Get被调用两次,因为那时它没有内存。

Xcode is just the IDE -- it shouldn't have any bearing on what happens when your app is executing. Xcode只是IDE - 它应该与你的应用程序执行时发生的事情有任何关系。 If there's a difference, it seems more likely that you're building for different iOS versions. 如果存在差异,那么您似乎更有可能为不同的iOS版本构建。

Did you set the view of the view controller? 您是否设置了视图控制器的视图? I had the same issue and I realized that I didn't set the view property. 我有同样的问题,我意识到我没有设置视图属性。

- (void)viewDidLoad {
    UIView *contentView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame];
    //add some stuff to contentView here
    self.view = contentView;
    [contentView release];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM