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

标签 cocoa installation application-design

我正在创建一个应用程序,我希望用户在首次应用程序启动期间设置一些强制性首选项。实现这一目标最常见的场景是什么?我应该设置一些用户默认值来查看应用程序是否已设置吗?另外 - 如果我确定该应用程序是第一次启动 - 我应该如何显示“设置”窗口?如果我从单独的 xib 文件加载它 - 我将如何延迟主应用程序窗口的显示?

最佳答案

执行此操作的标准方法是在主 Controller 类的 +(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

正是该标志决定了是否在 Nib 加载后立即显示窗口,或者是否需要使用诸如 makeKeyAndOrderFront: 之类的内容以编程方式执行此操作。

关于cocoa - 首次启动期间 Cocoa 应用程序设置的最常见场景是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4525781/

相关文章:

oracle - 通过设计克服 'log file sync' ?

iphone - 如何从 Xcode 项目中清理过时和排除的文件?

git - npm 模块仅在全局不可用时才在本地安装

.net - 寻找在我们的应用程序中实现版本控制功能的建议

java - 是否弃用了 DTO 模式?

node.js - 使用 npm 安装 log.io 时出错

cocoa - 如何访问QuickLook插件资源?

iphone - 如何暂停循环以请求输入 (iPhone)

objective-c - 如何让 NSSearchField 在自动完成时发送操作?

android - session 'app' : Error Installing APK