0

everyone!

So basically, I am trying to create a simple toggle out of my UIButton. With the code below, I am able to click the button, but only for a brief moment do I see @"Expense" as the title. It is probably a VERY simple mistake. Any help is appreciated!

- (IBAction)typeChanger {
if ([typeButton.titleLabel.text isEqual:@"Income"]) {
    typeButton.titleLabel.text = @"Expense";
}else if ([typeButton.titleLabel.text isEqual:@"Expense"]) {
    typeButton.titleLabel.text = @"Income";
}
}

Thanks in advance!

3
  • 1
    if ([typeButton.titleLabel.text isEqualToString:@"Income"]) write this in condition...
    – Vishal
    Feb 26, 2013 at 4:31
  • Oh, okay. Didn't know that! But the same problem occurs.
    – ranjha
    Feb 26, 2013 at 4:33
  • 2
    hey guys, [typeButton.titleLabel.text isEqual:@"Income"] is working
    – Venk
    Feb 26, 2013 at 4:38

3 Answers 3

2

Do like this,

- (IBAction)typeChanger {
if ([typeButton.titleLabel.text isEqual:@"Income"]) {
   [typeButton setTitle:@"Expense" forState:UIControlStateNormal];
}else if ([typeButton.titleLabel.text isEqual:@"Expense"]) {
    [typeButton setTitle:@"Income" forState:UIControlStateNormal];
}
}
1
  • I don't like this because (1) you're ignoring the pressed state (so visual disparities- different text when pressed- are possible) and (2) typeButton.titleLabel is supposed to be primarily to configure the button according to the docs... not for setting or querying the button's title values (as that's what setTitle:forState and titleForState: respectively are meant for). Feb 26, 2013 at 4:50
2

This may be very useful and handy. Try this out.

https://github.com/Brayden/UICheckbox

0

You use NSString's isEqualToString: method instead of isEqual for string comparisons (either should work, but it's faster according to the docs).

Also, you need to use setTitle:forState: to set the title of a UIButton... the two important states to set are UIControlStateNormal (not pressed) and UIControlStateHighlighted (for when it's pressed):

- (IBAction)typeChanger {
    if ([[typeButton titleForState:UIControlStateNormal] isEqualToString:@"Income"]) {
         [typeButton setTitle:@"Expense" forState:UIControlStateNormal];
         [typeButton setTitle:@"Expense" forState:UIControlStateHighlighted];

    } else if ([[typeButton titleForState:UIControlStateNormal] isEqualToString:@"Expense"]) {
         [typeButton setTitle:@"Income" forState:UIControlStateNormal];
         [typeButton setTitle:@"Income" forState:UIControlStateHighlighted];
    }
}

See also the docs on NSString:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

And on UIButton:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

5
  • Oh, okay. Didn't know that! But the same problem occurs.
    – ranjha
    Feb 26, 2013 at 4:36
  • You have to use UIButton's setTitle:forState: method to set the title state. While there are several states, UIControlStateNormal and UIControlStateHighlighted are most commonly set and most important. Feb 26, 2013 at 4:40
  • Okay, I see. It works, thanks again! Couple questions, though. UIControlStateHighlighted is when the button is being pressed? Also, when I want to compare the values, like if the title is equal to something, then how would that be done?
    – ranjha
    Feb 26, 2013 at 4:42
  • Would it be like if (typebutton.titleLabel.text == @"Income")?
    – ranjha
    Feb 26, 2013 at 4:43
  • Yeah, it's actually better to also use UIButton's titleForState: method to query what the title for a given state is. See revised answer again. Feb 26, 2013 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.