xcode 为什么我的应用程序委托的 didFinishLaunchingWithOptions 方法突然在我的 RootViewController:viewDidLoad 方法之后被调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2853589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:22:26  来源:igfitidea点击:

Why is my app delegate's didFinishLaunchingWithOptions method all of sudden being called AFTER my RootViewController:viewDidLoad method?

xcodeiphone-sdk-3.0interface-builderipad

提问by BeachRunnerFred

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...

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

AppNameAppDelegate.m

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

根视图控制器.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();
    }
}

When you build and run the project before making any changes at all, the application:didFinishLaunchingWithOptionsmethod is called before the RootViewController:viewDidLoadmethod 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...

当您在进行任何更改之前构建和运行项目时,会在application:didFinishLaunchingWithOptions调用RootViewController:viewDidLoad方法之前调用该方法。我是 iPhone 开发的新手,但我认为这是正确且典型的顺序。所以这是我所做的更改......

  • Once I confirmed everything was working without any modifications, I changed the RootViewController code and set it as a subclass of UIViewController(instead of UITableViewControllerby 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:viewDidLoadis being called before the application:didFinishLaunchingWithOptionsmethod.
  • 一旦我确认一切正常而没有任何修改,我更改了 RootViewController 代码并将其设置为UIViewController(而不是UITableViewController默认情况下)的子类,并在 Interface Builder 中进行了相应的调整。我构建并运行,一切仍然正常。
  • 然后,我在 IB 的 RootView 中添加了一个 UIView(其中没有任何内容),当我构建并运行它时,突然RootViewController:viewDidLoadapplication:didFinishLaunchingWithOptions方法之前调用了它。

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 managedObjectContextthat it uses to perform the fetch request.

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

  1. Any ideas what caused this?
  2. Any ideas how I can fix this?
  1. 任何想法是什么导致了这个?
  2. 有什么想法可以解决这个问题吗?

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

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

采纳答案by hooleyhoop

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

在模板app中-applicationDidFinishLaunching将RootViewController的视图添加到窗口中,导致视图加载,所以很明显-viewDidLoad会跟随-applicationDidFinishLaunching。

ViewDidLoad is (indirectly) called from applicationDidFinishLaunching.

ViewDidLoad 是(间接)从 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.

如果,如您所说,在 applicationDidFinishLaunching 之前调用 viewDidLoad,那是因为您在调用 applicationDidFinishLaunching 之前做了一些事情导致视图加载。

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

您是否在 -viewDidLoad 中添加了断点并查看堆栈跟踪以查看调用它的原因?

回答by kuuldor

This is propabaly caused by the fact that in MainWindow.xib, your application delegate object is not connected to File's Owner (UIApplication). You can open the MainWindow.xib and right click on your App Delegate to see if it has a connection in Referencing Outlet to File's Owner. If not, set it to. And this will fix your problem.

这可能是因为在 MainWindow.xib 中,您的应用程序委托对象未连接到文件所有者 (UIApplication)。您可以打开 MainWindow.xib 并右键单击您的 App Delegate 以查看它是否在引用出口到文件所有者中具有连接。如果没有,请设置为。这将解决您的问题。

回答by shosti

Where are you initializing RootViewController? Typically, you do so in applicationDidFinishLaunching(at least on the iPhone). If you are initializing it in your app delegate's initmethod, that could cause the root view controller's viewDidLoadmethod to be invoked before applicationDidFinishLaunching.

你在哪里初始化RootViewController?通常,您在applicationDidFinishLaunching(至少在 iPhone 上)这样做。如果您在应用程序委托的init方法中对其进行初始化,则可能会导致在viewDidLoad调用之前调用根视图控制器的方法applicationDidFinishLaunching.