NSPopUpButton - 如何有选择地禁用某些菜单项?

Sah*_*ani 5 cocoa objective-c


我的应用程序中有一个登录页面,有两个用户 - 管理员和标准版.成功登录后,我们会进入欢迎页面,其中有一个弹出按钮.我想在标准用户的弹出按钮菜单中禁用第3和第4个菜单项.
对于管理员,所有菜单项都应该可用.
我希望根据用户在登录页面上的选择进行区分.
请帮帮我,我怎么能做到这一点?
我必须使用KVC概念吗?如果有,怎么样?

提前致谢..

Emp*_*ack 13

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];
    
    Run Code Online (Sandbox Code Playgroud)
  2. Set autoenablesItems property of NSPopUpButton to NO.

    [yourPopUpButton setAutoenablesItems:NO];
    
    Run Code Online (Sandbox Code Playgroud)

    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
Run Code Online (Sandbox Code Playgroud)

UserType是用户定义的密钥.您可以定义所需的任何键.