首次启动期间 Cocoa 应用程序设置最常见的场景是什么?

发布于 2024-10-09 09:29:22 字数 164 浏览 1 评论 0原文

我正在创建一个应用程序,我希望用户在第一次应用程序启动期间设置一些强制性首选项。实现这一目标最常见的场景是什么?我应该设置一些用户默认值来查看应用程序是否已设置吗?另外 - 如果我确定该应用程序是第一次启动 - 我应该如何显示“设置”窗口?如果我从单独的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-10-16 09:29:22

执行此操作的标准方法是在主控制器类的 +(void)initialize 方法中。

例如,在您的界面 (.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

基本上,您在初始化方法中设置了所有默认值。 (initialize 方法在调用 init 之前就被调用,因此它提供了一个方便的位置来确保用户默认值都具有默认值)。 NSUserDefaultsregisterDefaults: 方法很特殊,因为只有在尚未设置特定值时才会使用您传入的值。换句话说,在上面的代码中,我在 applicationDidFinishLaunching: 方法中将第一个启动键设置为 NO,这将覆盖默认值并将保存到应用程序的首选项 plist 文件中。首选项文件中保存的值优先于您在 initialize 方法中使用用户默认值注册的值。

要推迟打开主窗口,您基本上需要确保在 Interface Builder 的属性检查器中关闭相关窗口的“启动时可见”标志:

alt text

这个标志决定是否在笔尖加载后立即显示窗口,或者是否需要使用诸如 makeKeyAndOrderFront:< 之类的内容以编程方式执行此操作/代码>。

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

For example, in your interface (.h):

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

Then in your .m file:

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];
}

line break

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

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.

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:

alt text

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文