在第一次启动时,Cocoa应用程序设置的最常见的情况是什么? [英] What's the most common scenario for Cocoa app setup during first launch?

查看:175
本文介绍了在第一次启动时,Cocoa应用程序设置的最常见的情况是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,我希望用户在首次应用程序启动时设置一些强制性偏好。最常见的情况是什么?我应该设置一些用户默认值,看看应用程序是否已设置?此外 - 如果我确定应用程序是第一次启动 - 我应该如何显示设置窗口?如果我从separte xib文件加载它 - 我如何deffer主应用程序窗口的显示?

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?

推荐答案

这是在你的主控制器类的 +(void)initialize 方法中。

The standard way to do this is in the +(void)initialize method of your main controller class.

interface(.h):

For example, in your interface (.h):

@interface MDAppController : NSObject {
    BOOL MDFirstRun;
    BOOL showInspector;
    BOOL showIcons;
}
@end

然后在.m文件中:

NSString * const MDFirstRunKey            = @"MDFirstRun";
NSString * const MDShouldShowInspectorKey  = @"MDShouldShowInspector";
NSString * const MDBrowserShouldShowIconsKey  = @"MDBrowserShouldShowIcons";

@implementation 

+ (void)initialize {
    NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];

    [defaultValues setObject:[NSNumber numberWithBool:YES]
                      forKey:MDFirstRunKey];

    [defaultValues setObject:[NSNumber numberWithBool:NO]
                      forKey:MDShouldShowInspectorKey];

    [defaultValues setObject:[NSNumber numberWithBool:YES]
                      forKey:MDBrowserShouldShowIconsKey];

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
    [[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaultValues];
}

换行符

- (id)init {
   if (self = [super init]) {
       NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

       MDFirstRun = [[userDefaults objectForKey:MDFirstRunKey] boolValue];
       showInspector = [[userDefaults objectForKey:MDShouldShowInspectorKey] boolValue];
       showIcons = [[userDefaults objectForKey:MDBrowserShouldShowIconsKey] boolValue];
   }
   return self;
}



- (void)applicationDidFinishLaunching:(NSNotification *)notification {
   if (MDFirstRun) {
     [[NSUserDefaults standardUserDefaults]
         setObject:[NSNumber numberWithBool:NO]
         forKey:MDFirstRunKey];

     // show first use panel

   } else {
     // do normal launch
   }
}

 /// other methods
@end

基本上,您的初始化方法中的默认值。 (初始化方法在调用 init 之前很早就调用,因此它提供了一个方便的地方,以确保用户默认值都具有默认值)。 方法 NSUserDefaults 的特殊之处在于,您传递的值仅在特定值hasn' t已设置。换句话说,当在上面的代码中,我在 applicationDidFinishLaunching:方法中将第一个启动键设置为NO,这将覆盖默认值,并将保存到您的应用程序的首选项plist文件。保存在首选项文件中的值优先于在初始化方法中使用用户默认值注册的值。

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). The registerDefaults: method of NSUserDefaults 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 the applicationDidFinishLaunching: 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 the initialize method.

要推迟打开主窗口,您基本上要确保在Interface Builder中的Attributes检查器中,对于所讨论的窗口,Visible at Launch标志已关闭:

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:

它是那个标志,决定一个窗口是否显示,一旦nib加载,或者你是否需要使用 makeKeyAndOrderFront:

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

这篇关于在第一次启动时,Cocoa应用程序设置的最常见的情况是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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