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..
You need to do two things here.
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];
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With