I'm trying to implement a system that changes a label based on the state of an NSPopUpButton
.
So far I've tried to do what's displayed in the code below, but whenever I run it, the code just jumps into the else
clause, throwing an alert
- (IBAction)itemChanged:(id)sender {
if([typePopUp.stringValue isEqualToString: @"Price per character"]) {
_currency = [currencyField stringValue];
[additionalLabel setStringValue: _currency];
}
else if([typePopUp.stringValue isEqualToString: @"Percent saved"]) {
_currency = additionalLabel.stringValue = @"%";
}
else alert(@"Error", @"Please select a calculation type!");
}
So does anyone here know what to do to fix this?
@hamstergene is on the right track, but is comparing the title of the menu item rather than, say, the tag, which is wrong for the following reasons:
Having said all that,
NSPopUpButton
makes it difficult to insert tags into the menu items, so you need to use the index of the selected item:Assume you create the menu items using:
Then create an
enum
that matches the order of the titles in the array:And then compare the selected item index, as follows:
NOTE the following line was incorrect in your code:
Multiple assignment works because the result of
x = y
isy
. This is not the case when a setter is involved. The corrected code is above.EDIT This answer was heavily edited following more info from the OP.