MASPreferences选择视图

时间:2015-06-18 09:16:02

标签: objective-c nswindow

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

2 个答案:

答案 0 :(得分:1)

经过一番尝试,并使用@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];
}

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

答案 1 :(得分:0)

MASPreferences会记住上次打开的标签'

将数据传递到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;
}

查看MASPReferencesWindowController.m第6行。有一个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

- (void)setSelectedViewController:(NSViewController <MASPreferencesViewController> *)controller

中查找MASPreferencesWindowController.m方法

评论此行:

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