MASPreferences select view

I am using MASPreferences in my application, I was able to set everything up correctly using 3 different views (preferences, login and about). What I would like to do is choose which panel will be displayed when the window is opened. This means that when the user clicks, the panel is displayed, etc., rather than the last displayed panel. At the moment, I tried to change the entry in the plist file, but it doesn't seem to work. Is there another way?

+3


source to share


2 answers


So, after a bit of trying, and using @ Jasper's answer, I came up with the following:

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

      



This method essentially writes the required "tab" to the plist file and then initializes a new instance each time. This loads the correct view. The ID parameter is the one you configured for each of the species. Thanks again to Jasper for his answer, really helped me figure out how to figure this out!

+1


source


MASPreferences

remembers the last open tab

Changing the order in your array while passing it to yours MASPreferencesWindowController

should work to change the order of your tabs.

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

      

Look inside line MASPReferencesWindowController.m

6. There is a key static NSString

that handles the logic to display the last selected tab

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

The key is used in:

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

Look for a method - (void)setSelectedViewController:(NSViewController <MASPreferencesViewController> *)controller

insideMASPReferencesWindowController.m

Comment this line:

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

      

Now change the way you initialize yours NSWindowController

to create a new instance each time, otherwise it will remember the last tab selected:

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

      

0


source







All Articles