为什么我的应用委托的didFinishLaunchingWithOptions方法突然被我的RootViewController调用:viewDidLoad方法? [英] Why is my app delegate's didFinishLaunchingWithOptions method all of sudden being called AFTER my RootViewController:viewDidLoad method?

查看:105
本文介绍了为什么我的应用委托的didFinishLaunchingWithOptions方法突然被我的RootViewController调用:viewDidLoad方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Xcode中的iPad的SplitView模板。以下是基于拆分视图的应用程序模板为您自动生成的许多重要方法中的两个...

I've been playing with the iPad's SplitView template in Xcode. Here are two of the many important methods that are auto-generated for you by the Split View-based Application template...

AppNameAppDelegate.m

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after app launch    
    rootViewController.managedObjectContext = self.managedObjectContext;


    // Add the split view controller's view to the window and display.
    [window addSubview:splitViewController.view];
    [window makeKeyAndVisible];

    return YES;
}

RootViewController.m

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {

    [super viewDidLoad];
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
}

在进行任何更改之前构建并运行项目时在调用 RootViewController:viewDidLoad 方法之前,调用应用程序:didFinishLaunchingWithOptions 方法。我是iPhone开发的新手,但我认为这是正确和典型的序列。所以这里是我所做的更改......

When you build and run the project before making any changes at all, the application:didFinishLaunchingWithOptions method is called before the RootViewController:viewDidLoad method is called. I'm new to iPhone development, but I'm assuming this is the correct and typical sequence. So here are the changes I made...


  • 一旦我确认一切正常工作而没有任何修改,我更改了RootViewController代码并设置了它作为 UIViewController 的子类(默认情况下代替 UITableViewController )并在Interface Builder中进行相应的调整。我建立并运行,一切仍然正常。

  • 然后,我在IB中的RootView中添加了一个UIView(其中没有任何内容),当我构建并运行它时,突然 RootViewController:viewDidLoad 应用程序之前调用c $ c>:didFinishLaunchingWithOptions 方法。

  • Once I confirmed everything was working without any modifications, I changed the RootViewController code and set it as a subclass of UIViewController (instead of UITableViewController by default) and made the respective adjustments in Interface Builder. I built and ran, everything was still working fine.
  • Then, I added a UIView (with nothing in it) to the RootView in IB and when I built and ran it, suddenly the RootViewController:viewDidLoad is being called before the application:didFinishLaunchingWithOptions method.

我需要让它恢复到以前的工作方式,因为正如你在代码中看到的那样,viewDidLoad方法取决于在didFinishLauchingWithOptions方法上执行所以它可以设置rootViewController的 managedObjectContext 它用来执行获取请求。

I need to get it back to the way it was working before because, as you can see in the code, the viewDidLoad method depends on didFinishLauchingWithOptions method to execute so it can set the rootViewController's managedObjectContext that it uses to perform the fetch request.


  1. 是什么原因造成的?

  2. 我有什么想法可以解决这个问题吗?

非常感谢您的帮助!我会继续研究和与代码打。

Thanks so much in advance for your help! I'm gonna keep researching and playing with the code.

推荐答案

在模板应用-applicationDidFinishLaunching增加RootViewController的的视图窗口,导致视图加载,所以显然-viewDidLoad将遵循 - applicationDidFinishLaunching。

In the template app -applicationDidFinishLaunching adds RootViewController's view to the window, causing the view to load, so obviously -viewDidLoad will follow - applicationDidFinishLaunching.

从applicationDidFinishLaunching(间接)调用ViewDidLoad。

ViewDidLoad is (indirectly) called from applicationDidFinishLaunching.

如果你说的话,之前调用viewDidLoad在applicationDidFinishLaunching那是因为你做了一些事,以使视图加载的applicationDidFinishLaunching被调用之前。

If, as you say, viewDidLoad is being called before applicationDidFinishLaunching it is because you have done something to cause the view to load before applicationDidFinishLaunching is called.

你在-viewDidLoad添加断点,并期待在堆栈跟踪,看看有什么是谁负责打电话?

Did you add a breakpoint in -viewDidLoad and look at the stacktrace to see what was responsible for calling it?

这篇关于为什么我的应用委托的didFinishLaunchingWithOptions方法突然被我的RootViewController调用:viewDidLoad方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆