首次启动期间 Cocoa 应用程序设置最常见的场景是什么?
我正在创建一个应用程序,我希望用户在第一次应用程序启动期间设置一些强制性首选项。实现这一目标最常见的场景是什么?我应该设置一些用户默认值来查看应用程序是否已设置吗?另外 - 如果我确定该应用程序是第一次启动 - 我应该如何显示“设置”窗口?如果我从单独的 xib 文件加载它 - 我将如何延迟主应用程序窗口的显示?
I am creating an app and I would like a user to set some obligatory preferences during first app launch. What is the most common scenario to achieve this? Should I set some user defaults to see if the app has been setup? Also - if I determine that the app is being launched for the first time - how should I display "Setup" window? If I load it from the separte xib file - how will I deffer the display of main app window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
执行此操作的标准方法是在主控制器类的
+(void)initialize
方法中。例如,在您的界面 (.h) 中:
然后在您的 .m 文件中:
换行符
基本上,您在初始化方法中设置了所有默认值。 (initialize 方法在调用
init
之前就被调用,因此它提供了一个方便的位置来确保用户默认值都具有默认值)。NSUserDefaults
的registerDefaults:
方法很特殊,因为只有在尚未设置特定值时才会使用您传入的值。换句话说,在上面的代码中,我在 applicationDidFinishLaunching: 方法中将第一个启动键设置为 NO,这将覆盖默认值并将保存到应用程序的首选项 plist 文件中。首选项文件中保存的值优先于您在initialize
方法中使用用户默认值注册的值。要推迟打开主窗口,您基本上需要确保在 Interface Builder 的属性检查器中关闭相关窗口的“启动时可见”标志:
这个标志决定是否在笔尖加载后立即显示窗口,或者是否需要使用诸如
makeKeyAndOrderFront:< 之类的内容以编程方式执行此操作/代码>。
The standard way to do this is in the
+(void)initialize
method of your main controller class.For example, in your interface (.h):
Then in your .m file:
line break
Basically, you set up all of your default values in your initialize method. (The initialize method is called very early on before
init
is called, so it provides a convenient place to make sure user defaults all have default values). TheregisterDefaults:
method ofNSUserDefaults
is special in that the values you pass in only are used if a particular value hasn't already been set. In other words, when in the code above, I set the first launch key to NO in theapplicationDidFinishLaunching:
method, that overrides the default value and will be saved to your application's preferences plist file. The values that are saved in the preferences file take precedence over those that you've registered with user defaults in theinitialize
method.To defer opening of the main window, you basically want to make sure that the "Visible at Launch" flag is turned off for the window in question in the Attributes inspector in Interface Builder:
It's that flag that determines whether a window is shown as soon as the nib is loaded, or whether you will need to do it programmatically using something like
makeKeyAndOrderFront:
.