views:

95

answers:

1

I have a method here that is in my code:

-(IBAction) actionButtonPressed: (id) sender{
    NSLog(@"%@",actionButton.titleLabel.text);
    if (actionButton.titleLabel.text == @"Begin Recording"){
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateNormal];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateApplication];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateHighlighted];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateReserved];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateSelected];
        [actionButton setTitle:@"Finish Recording" forState:UIControlStateDisabled];
        UIImage *redButton = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bigredbutton" ofType:@"png"]];
        [actionButton setBackgroundImage:redButton forState:UIControlStateNormal];
        [actionButton setBackgroundImage:redButton forState:UIControlStateApplication];
        [actionButton setBackgroundImage:redButton forState:UIControlStateHighlighted];
        [actionButton setBackgroundImage:redButton forState:UIControlStateReserved];
        [actionButton setBackgroundImage:redButton forState:UIControlStateSelected];
        [actionButton setBackgroundImage:redButton forState:UIControlStateDisabled];
    }
    if (actionButton.titleLabel.text == @"Finish Recording"){
        [self dismissModalViewControllerAnimated:YES];
    }

}

For some reason the NSLog call does work and shows up on the Console. The button starts out reading Begin Recording, yet pressing it does nothing even though it is tied up with Touch Up Inside in Interface Builder to call this method and is referenced.

+3  A: 

Your problem is this line:

if (actionButton.titleLabel.text == @"Begin Recording"){

There you're comparing pointers, not strings. You need:

if ([actionButton.titleLabel.text isEqualToString:@"Begin Recording"]){
Dave DeLong
This is mostly correct, but I think you should use isEqualToString, not isEqual. To expand a bit on what Dave said, the == operator will only evaluate to true when comparing constant strings, and it's still bad form to use it that way. Use isEqualToString for all string comparisons.
Andrew Johnson
@Andrew - fair enough. Edited answer. (Although I've never had problems with just using `isEqual:`)
Dave DeLong
Yeah, I kind of thought isEqual would work because it was you saying it, but I've never used it before... I wonder if their is any difference.
Andrew Johnson