MASPreferences 选择视图

MASPreferences choose view

我在我的应用程序中使用 MASPreferences,我能够正确设置所有内容,有 3 个不同的视图(首选项、登录和关于)。我想做的是选择哪个面板得到打开 window 时显示。这样一来,当用户单击关于时,将显示关于面板等,而不是最后显示的面板。到目前为止,我已经尝试修改 plist 文件中的条目,但它似乎不起作用。还有其他办法吗?

MASPreferences 记住上次打开的'tab'

将数组传递给您时更改数组中的顺序 MASPreferencesWindowController 应该可以更改选项卡的顺序。

-(NSWindowController *)preferencesWindowController
{
    if (_preferencesWindowController == nil)
    {
        NSViewController *generalViewController = [[GeneralPreferencesViewController alloc] init];
        NSViewController *accountViewController = [[AccountPreferencesViewController alloc] init];
        NSViewController *troubleshootingViewController = [[TroubleShootingPreferencesViewController alloc] init];

        //Change the order here
        NSArray *controllers = [[NSArray alloc] initWithObjects:accountViewController, generalViewController, troubleshootingViewController, nil];

        NSString *title = NSLocalizedString(@"Preferences", @"Common title for Preferences window");
        _preferencesWindowController = [[MASPreferencesWindowController alloc] initWithViewControllers:controllers title:title];
    }
    return _preferencesWindowController;
}

查看第 6 行 MASPReferencesWindowController.m。有一个 static NSString 键处理显示最后选择的选项卡的逻辑

static NSString *const kMASPreferencesSelectedViewKey = @"MASPreferences Selected Identifier View";

密钥用于:

- (void)windowDidLoad
{
    if ([self.title length] > 0)
        [[self window] setTitle:self.title];

    if ([self.viewControllers count])
        self.selectedViewController = [self viewControllerForIdentifier:[[NSUserDefaults standardUserDefaults] stringForKey:kMASPreferencesSelectedViewKey]] ?: [self firstViewController];

    NSString *origin = [[NSUserDefaults standardUserDefaults] stringForKey:kMASPreferencesFrameTopLeftKey];
    if (origin)
        [self.window setFrameTopLeftPoint:NSPointFromString(origin)];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidMove:)   name:NSWindowDidMoveNotification object:self.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:self.window];
}

TL;DR

MASPreferencesWindowController.m

里面寻找方法- (void)setSelectedViewController:(NSViewController <MASPreferencesViewController> *)controller

评论这一行:

// Record new selected controller in user defaults
[[NSUserDefaults standardUserDefaults] setObject:controller.identifier forKey:kMASPreferencesSelectedViewKey];

现在更改初始化 NSWindowController 的方式,以便每次都创建一个新实例,否则它仍会记住上次选择的选项卡:

-(NSWindowController *)preferencesWindowController
{
    NSViewController *generalViewController = [[GeneralPreferencesViewController alloc] init];
    NSViewController *accountViewController = [[AccountPreferencesViewController alloc] init];
    NSViewController *troubleshootingViewController = [[TroubleShootingPreferencesViewController alloc] init];
    //NSArray *controllers = [[NSArray alloc] initWithObjects:generalViewController, accountViewController, troubleshootingViewController, nil];
    NSArray *controllers = [[NSArray alloc] initWithObjects:accountViewController, generalViewController, troubleshootingViewController, nil];

    // To add a flexible space between General and Advanced preference panes insert [NSNull null]:
    //     NSArray *controllers = [[NSArray alloc] initWithObjects:generalViewController, [NSNull null], advancedViewController, nil];


    NSString *title = NSLocalizedString(@"Preferences", @"Common title for Preferences window");
    _preferencesWindowController = [[MASPreferencesWindowController alloc] initWithViewControllers:controllers title:title];

    return _preferencesWindowController;
}

所以经过一些尝试,并通过使用@Jasper 的回答,我想出了以下内容:

-(void)openPreferencesWindowWithIdentifier:(NSString *)identifier {
    [NSApp activateIgnoringOtherApps:YES];
    [[NSUserDefaults standardUserDefaults] setValue:identifier forKey:@"MASPreferences Selected Identifier View"];

    // Create the preferences window
    NSViewController *generalViewController = [[GeneralPreferencesViewController alloc]initWithNibName:@"GeneralPreferencesViewController" bundle:nil];
    NSViewController *loginViewController = [[PushoverLoginViewController alloc]initWithNibName:@"PushoverLoginViewController" bundle:nil];
    NSViewController *aboutViewController = [[AboutPreferencesViewController alloc]initWithNibName:@"AboutPreferencesViewController" bundle:nil];
    NSArray *controllers = [[NSArray alloc]initWithObjects:generalViewController,loginViewController,[NSNull null],aboutViewController, nil];
    NSString *windowTitle = NSLocalizedString(@"Preferences", @"Comon title for preferences window");
    _preferencesWindowController = [[MASPreferencesWindowController alloc]initWithViewControllers:controllers title:windowTitle];

    [self.preferencesWindowController showWindow:nil];
}

这个方法本质上是把需要的"tab"写入plist文件,然后每次都初始化一个新的实例。通过这样做,加载了正确的视图。标识符参数是您为每个视图设置的参数。再次感谢 Jasper 的回答,真的帮助我理解了如何解决这个问题!