Noor Syed
Noor Syed

Reputation: 630

UIButton's Selected State not working in iOS7

I am using Xcode 5's storyboarding to create an application, iOS SDK 7.0.

I dragged a button to the View. I want my button to display "A" by Default. Below are the button properties: Type: System, State Config: Default, Title: Plain

I want my button to display "B" in selected state. So, the button properties that changed: State Config: Selected

In the Button's Attribute inspector, under the Control, there is a property called Selected. If I check Selected, instead of displaying "B", it just highlights like a marker over A, i.e, no character is seen.

I ran it in XCODE. WHen the button is not selected "A" is displayed as expected but when I select the button, instead of displaying B, the text portion of the button is highlighted with light blue color. Also, I never selected the Highlighted Content under the Control.

How can this be resolved, so that I can display the state correctly?

Upvotes: 2

Views: 13557

Answers (3)

Chris Chen
Chris Chen

Reputation: 5407

For anyone who set the button title by code, I found on iOS 7.1, you need to explicitly set title for various button states, while on iOS 7.0, setting title for UIControlStateNormal will also do for other button states.

// works on 7.0
[self.ibActionButton setTitle:NSLocalizedString(@"Register", nil) forState:UIControlStateNormal];

// works on 7.1
[self.ibActionButton setTitle:NSLocalizedString(@"Register", nil) forState:UIControlStateNormal|UIControlStateDisabled];

Upvotes: 6

Noor Syed
Noor Syed

Reputation: 630

I found that this is a bug in XCODE 5, that is, when we check the selected property under Control for the button, it doesn't show the selected state on the Button. It works in the simulator we run the application. We would need to do a target-action for the button and toggle the selected state as below

 - (IBAction)flipButton:(UIButton *)sender {
      sender.selected=!sender.isSelected;
   }

Upvotes: 3

Jonathan Arbogast
Jonathan Arbogast

Reputation: 9650

You need to set the title of the button to 'B' while you have State Config set to Selected in your storyboard. Then you need to connect an action to the button's Touch Up Inside event. Inside of that action, toggle the button's selected property.

When you check 'Selected' under the Attributes Inspector, you are just setting the button's initial selected state to YES. XCode also shows you what the button would look like in that state.

Upvotes: 5

Related Questions