OS X故事板在applicationDidFinishLaunching之前调用viewDidLoad [英] OS X storyboard calls viewDidLoad before applicationDidFinishLaunching

查看:175
本文介绍了OS X故事板在applicationDidFinishLaunching之前调用viewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用情节提要板在Xcode中创建了Mac OS X应用程序.由于某些原因,在NSViewControllers中的viewDidLoad之后调用AppDelegate中的applicationDidFinishLaunching方法.与iOS应用程序一样,我认为viewDidLoad应该在applicationDidFinishLaunching之前调用? OS X应用程序中的情节提要板是否在应用程序完成启动之前初始化视图控制器?

I created a Mac OS X application in Xcode using storyboards. For some reason the applicationDidFinishLaunching method in the AppDelegate is being called after viewDidLoad in the NSViewControllers. As with iOS apps, I thought viewDidLoad is supposed to be called before applicationDidFinishLaunching? Do storyboards in OS X apps initialize the view controllers before the app has finished launching?

我正在使用applicationDidFinishLaunching方法将默认设置注册到NSUserDefaults中.不幸的是,在加载情节提要中的视图之后,会发生默认值的注册.因此,当我使用viewDidLoad在每个视图控制器中设置视图时,尚未设置NSUserDefaults中的默认数据.如果我不能使用applicationDidFinishLaunching在OS X故事板应用程序中注册NSUserDefaults,那么在调用viewDidLoad之前如何设置默认值?

I am using the applicationDidFinishLaunching method to register default settings into NSUserDefaults. Unfortunately, registering the default values is happening after the views in the storyboard are loaded. Therefore, when I set up the view in each view controller using viewDidLoad, the defaults data in NSUserDefaults has not been set. If I can't use applicationDidFinishLaunching to register NSUserDefaults in OS X storyboard apps, then how I set the defaults before viewDidLoad is called?

为解决此问题,在Xcode的Main.storyboard中,我关闭了主窗口的是初始控制器".我向主窗口分配了一个故事图板ID作为"MainWindow".然后在AppDelegate中输入以下代码:

To fix this issue, in the Main.storyboard in Xcode, I turned off "Is Initial Controller" for the main window. I assigned a storyboard ID to the main window as "MainWindow". Then in the AppDelegate I entered the following code:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        let storyboard = NSStoryboard(name: "Main", bundle: nil)
        let mainWindow = storyboard.instantiateControllerWithIdentifier("MainWindow") as! NSWindowController
        mainWindow.showWindow(nil)
        mainWindow.window?.makeKeyAndOrderFront(nil)

    }
}

该应用程序不会崩溃,但是现在该窗口不再显示.下图显示了我正在使用的情节提要:

The app does not crash but now the window never appears. The following image displays the storyboard I'm working with:

推荐答案

正确,生命周期在OS X中略有不同.

Correct, the lifecycle is a wee bit different in OS X.

代替让故事板作为您的初始界面(在项目的常规设置中定义),您可以设置MainMenu xib文件并将其指定为主界面,然后在AppDelegate的applicationDidFinishLaunching方法中进行设置您可以在完成其他初始化代码后以编程方式实例化情节提要.

Instead of letting the storyboard be your initial interface (this is defined in the General settings of your project), you can instead set up a MainMenu xib file and designate that as your main interface, then in your applicationDidFinishLaunching method in your AppDelegate you can programmatically instantiate your storyboard after you have completed your other initialization code.

如果您还没有的话,我建议您查看 OS X的可可编程:The Big Nerd Ranch Guide ;他们在书中所做的一件好事实际上是让您摆脱了一些默认的Xcode模板内容,取而代之的是,他们通过显式地以正确"的方式设置了初始视图控制器.

I recommend checking out Cocoa Programming for OS X: The Big Nerd Ranch Guide if you haven't already; one nice thing they do in their book is actually have you get rid of some of the default Xcode template stuff and instead they have you set up your initial view controller the "right" way by doing it explicitly.

您可以在您的applicationDidFinishLaunching中放置这样的内容:

You might put something like this in your applicationDidFinishLaunching:

NSStoryboard *mainStoryboard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
MyWindowController *initialController = (MyWindowController *)[mainStoryboard instantiateControllerWithIdentifier:@"myWindowController"];
[initialController showWindow:self];
[initialController.window makeKeyAndOrderFront:self];

这假定您已经将主界面"更改为MainMenu.xib之类的内容.

This assumes that you've already changed "Main Interface" to something like MainMenu.xib.

这篇关于OS X故事板在applicationDidFinishLaunching之前调用viewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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