SetAction for NSPopUpButton disables my popUpButton

I created my NSPopUpButton programmatically with the following code

[myPopUpButton insertItemWithTitle:@"--Select one--" atIndex:0]; [myPopUpButton addItemsWithTitles:[NSArray arrayWithObjects:@"1.One",@"Two",@"Three", nil]]; [myPopUpButton sizeToFit]; [myPopUpButton setAction:@selector(popUpAction:)]; [fullBrowserView addSubview: myPopUpButton]; //PopUp Action -(void)popUpAction:(id)sender { NSLog(@"popUpAction"); } 

When I click the popUpButton button, the popUpButton menu items are disabled. When I use interfacebuilder, it just works fine with IBAction.

Why does this setAction not work for NSPopUpButton?

+6
source share
2 answers

It looks like you are not setting the target to send the message to. So in the code add:

 [myPopUpButton setTarget:self]; 

assuming the popUpAction: method is in the same class.

When you use Interface Builder, it connects the selector action to the target.

From the documentation for this call:

- (void)setTarget:(id)anObject

If anObject is nil , but the control still has a valid action message, the application follows the responder chain, looking for an object that can respond to the message.

In your case, there is no object responding to the message.

+12
source

Even if myPopUpButton has a goal and an action, you might also need to add:

 [myPopUpButton setAutoenablesItems:NO]; 

Otherwise, every time the button is pressed, it can automatically turn off all the items in its menu. (I understand this question is out of date, but publishes this solution if it helps others).

+2
source

Source: https://habr.com/ru/post/910162/


All Articles