Wie der Name des Absenders eines NSButton erhalten
-
09-10-2019 - |
Frage
Ich habe mehrere NSButtons, die an einem einzigen IBAction befestigt sind. Ich muss innerhalb der Methode zwischen den verschiedenen Tasten unterscheiden. Ich habe versucht, die folgenden, aber es funktioniert nicht:
for (int i = 0; i++; i < 7) {
if (sender == [NSString stringWithFormat:@"button%i", i+1])
{
NSLog(@"sender is button %i", i+1);
}
}
Wie kann diese an die Arbeit gemacht werden?
Lösung
-(IBAction)buttonPressed:(id)sender
{
switch ( [sender tag] )
{
case 1:
//blah blah blah
break;
case 2:
//blah blah etc.
break;
}
}
I'm averse to doing the work for you, but....
replace this line
if (sender == [NSString stringWithFormat:@"button%i", i+1])
with this line
if ([sender tag] == i)
Note too that the format of your for loop is invalid:
for (int i = 0; i++; i < 7)
s/b:
for (int i = 0; i < 7; i++)
Andere Tipps
the sender in the btnClicked action is the button object that was clicked. From that you should be able to get the information you need
-(IBAction) btnClicked: (id) sender {
NSLog(@"Button clicked %@", sender);
// Do something here with the variable 'sender'
}
If you store a value in the sender.tag
, you can determine the button that way also
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow