Question

I have 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,menu items of popUpButton are disabled. When I use interfacebuilder,just it is working fine with the IBAction.

Why this setAction is not working for NSPopUpButton?

Was it helpful?

Solution

Looks like you're not setting a target object to send the message to. So, in code, add:

[myPopUpButton setTarget:self];

assuming the popUpAction: method is in the same class.

When you're using Interface Builder it's wiring-up 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 assigned, 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.

OTHER TIPS

Even if myPopUpButton has a target and action, you might also need to do add:

[myPopUpButton setAutoenablesItems:NO];

Otherwise, each time the button is clicked, it can automatically disable all items on its menu. (I realize this question is old, but posting this solution in case it helps others).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top