I'm just learning how to code so thanks for your patience on this simple question.
Here's my code:
- (IBAction)buttonWasPressed:(id)sender {
NSString *buttonName = [sender titleForState:UIControlStateNormal];
if (buttonName == @"Button 1") {
do something
}
How do I compare the title of the button passed as sender to a string?
Much thanks for the help.
in objective-c you can't compare strings using "==",
instead you should use the method isEqualToString
from the NSString class to compare a string with another.
if ([buttonName isEqualToString: @"Button 1"]) {
// do something
}
Use -isEqualToString method:
if ([buttonName isEqualToString:@"Button 1"])
...
using ==
you compare ponters, not the actual string values they contain
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With