Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPopUpButton-How to selectively disable some menu items?


I have a login page in my application with two users-Administrator and Standard. On successful login, we get to the welcome page, where a pop up button is present. I want to disable the 3rd and the 4th menu items in the pop up button menu for the standard user.
For Administrator, all the menu items should be available.
I want this distinction based on the user's selection on the login page.
Please help me, how i can achieve this?
Do i have to use KVC concepts? If yes, how?

Thanks in advance..

like image 346
Sahitya Tarumani Avatar asked Feb 02 '11 08:02

Sahitya Tarumani


2 Answers

You need to do two things here.

  1. To disable your third and fourth menu items, you need to set their enabled property to NO.

    [[yourPopUpButton itemAtIndex:2] setEnabled:NO];
    [[yourPopUpButton itemAtIndex:3] setEnabled:NO];
    
  2. Set autoenablesItems property of NSPopUpButton to NO.

    [yourPopUpButton setAutoenablesItems:NO];
    

    If you don't set this, the menu items will be automatically enabled when you click the pop up button, even though you disable them using Step 1.

Do these things in the -(void)awakeFromNib method.


For storing the login data to a persistent storage, you can use NSUserDefaults. For example.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; // LINE 1: create userDefaults instance
[userDefaults setObject:@"enteredUserType" forKey:@"UserType"]; // LINE 2: store a value
NSString *userType = [userDefaults objectForKey:@"UserType"]; // LINE 3: retrieve the value

Here, UserType is a user defined Key. You can define any key you want.

like image 138
EmptyStack Avatar answered Oct 14 '22 23:10

EmptyStack


Swift 4:

// Example NSPopUpButton
@IBOutlet weak var examplePopUpButton: NSPopUpButton!

// Set programatically or in Interface Builder
examplePopUpButton.autoenablesItems = false

// Change programmatically as needed
if let menuitem = examplePopUpButton.item(at: someIndex) {
    menuitem.isEnabled = false // or true
}
like image 27
l --marc l Avatar answered Oct 14 '22 22:10

l --marc l