xcode UIButton 的选定状态在 iOS7 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19198858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton's Selected State not working in iOS7
提问by Noor Syed
I am using Xcode 5's storyboarding to create an application, iOS SDK 7.0.
我正在使用 Xcode 5 的故事板创建应用程序 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
我拖了一个按钮到视图。我希望我的按钮默认显示“A”。以下是按钮属性:类型:系统,状态配置:默认,标题:普通
I want my button to display "B" in selected state. So, the button properties that changed: State Config: Selected
我希望我的按钮在选定状态下显示“B”。因此,更改的按钮属性:状态配置:选定
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.
在按钮的属性检查器中,在控件下,有一个名为 Selected 的属性。如果我选中 Selected,而不是显示“B”,它只是像 A 上的标记一样突出显示,即没有看到任何字符。
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.
我在 XCODE 中运行它。当未选择按钮时,“A”按预期显示,但是当我选择按钮时,而不是显示 B,按钮的文本部分以浅蓝色突出显示。此外,我从未选择控件下的突出显示内容。
How can this be resolved, so that I can display the state correctly?
如何解决这个问题,以便我可以正确显示状态?
回答by Chris Chen
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.
对于任何通过代码设置按钮标题的人,我发现在 iOS 7.1 上,您需要为各种按钮状态显式设置标题,而在 iOS 7.0 上,为 UIControlStateNormal 设置标题也适用于其他按钮状态。
// 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];
回答by Jonathan Arbogast
You need to set the titleof the button to 'B' while you have State Config set to Selectedin 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.
您需要将按钮的标题设置为“B”,同时在情节提要中将“状态配置”设置为“已选择”。然后你需要将一个动作连接到按钮的 Touch Up Inside 事件。在该操作中,切换按钮的selected
属性。
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.
当您在 Attributes Inspector 下选中“Selected”时,您只是将按钮的初始选定状态设置为 YES。XCode 还会显示按钮在该状态下的外观。
回答by Noor Syed
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
我发现这是XCODE 5中的一个bug,就是当我们为按钮检查Control下的selected属性时,没有在Button上显示selected状态。它在我们运行应用程序的模拟器中工作。我们需要为按钮做一个目标动作并切换选中状态,如下所示
- (IBAction)flipButton:(UIButton *)sender {
sender.selected=!sender.isSelected;
}