0

In Xcode, I'm trying to make a button that changes it's text, relative to what the text currently is. For example: If the button says "1" and it is pressed, I want the text to change to "2" and "2" to "3" and so forth, here's the snippet of code that's giving me trouble:

if (magicButton.titleLabel = @"1") {
    [magicButton setTitle:@"2" forState:UIControlStateNormal];
}

Xcode gives me this error "Assignment to readonly property" on line one of the snippet. I'm pretty new to Objective-C and iPhone App development, so maybe it's something crazily obvious and simple. Please don't mind if that's the case.

Here's a paste of my implementation file if it would help at all.

Thanks in advance.

Matthew
  • 3
  • 1
  • 1
  • 2
  • possible duplicate of [Why is my comparing if statement not working?](http://stackoverflow.com/questions/484709/, http://stackoverflow.com/questions/1547806/, http://stackoverflow.com/questions/881335/, http://stackoverflow.com/questions/8625936, http://stackoverflow.com/questions/11348809/, – jscs Jul 08 '12 at 20:04

2 Answers2

5

'=' is for assignment while '==' is for comparison. But in the case of string comparison you should use isEqualToString method. Something like this:

if ([magicButton.titleLabel.text isEqualToString: @"1"]) {    
  [magicButton setTitle:@"2" forState:UIControlStateNormal];
}

PS. Also note that you should get the UILabel's text property

Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • Hmm, it sais "No visible @interface file for 'UILabel' declares the selector 'isEqualToString' – Matthew Jul 08 '12 at 20:10
  • That's why I added the PS. It's `magicButton.titleLabel.text` that you should be comparing. – Alladinian Jul 08 '12 at 20:14
  • Oh okay. Thanks. EDIT: After compiling and running whenever I press the button iPhone simulator crashes. Ant thoughts on why this is? BTW You're an awesome help to me, Thank you so much on lifting me from my noobiness a little :) – Matthew Jul 08 '12 at 20:17
  • By the way my statement looks like this: `if ([magicButton.titleLabel.text isEqualToString: @"1"]) { [magicButton setTitle:@"2" forState:UIControlStateNormal]; }` – Matthew Jul 08 '12 at 20:28
  • Well... Are you sure that you have an `IBOutlet` set correctly to `magicButton`? Also if the same button is the one that calls the `buttonChange:` method, you could do something like this: `if ([sender.titleLabel.text isEqualToString: @"1"]) { [sender setTitle:@"2" forState:UIControlStateNormal]; }` – Alladinian Jul 08 '12 at 20:33
  • Yeah, it was a problem with my IBOutlets. Thank you so much for your help. I learned something today! You're great. – Matthew Jul 08 '12 at 20:40
  • @Matthew No problem. Consider marking the answer as correct if it worked for you. – Alladinian Jul 08 '12 at 20:50
0

If you want to be changing the button text relative to what it is with no restrictions, you can't make a million if statements. You should get the value of the button title (if it even has a title) and just add 1 to it, like so:

NSString *string = randomButton.titleLabel.text;

    if ([randomButton.titleLabel.text length] == 0) { //Check if there is not a title on the button

     [randomButton setTitle:@"1" forState:UIControlStateNormal]; //And if there isn't, set it to "1"

    }

    else {

        int yourInt = [string intValue]; //Convert to int

        int nextInt = yourInt + 1; //Add one to value

        NSString *finalString = [NSString stringWithFormat:@"%d",nextInt]; //Convert back to string

        [randomButton setTitle:finalString forState:UIControlStateNormal]; //Finally set it as the title

    }
Imirak
  • 1,323
  • 11
  • 21