ios 手动设置 UIButton 状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/405134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:28:51  来源:igfitidea点击:

Manually setting a UIButton state

iosobjective-ciphonecocoa-touchuibutton

提问by acreek

I UIButtonusing + buttonWithType:

UIButton使用 +buttonWithType:

What I need to figure out is how to manually change the button state. There are times when I need it to be set to "disabled."

我需要弄清楚的是如何手动更改按钮状态。有时我需要将其设置为“禁用”。

I read through the UIButtondocumentation but I cannot seem to find anything about manually setting a button state.

我通读了UIButton文档,但似乎找不到有关手动设置按钮状态的任何信息。

Any thoughts would be greatly appreciated.

任何想法将不胜感激。

回答by Ben Gottlieb

Did you try button.enabled = NO;?

你试了button.enabled = NO;吗?

Swift 5.0

斯威夫特 5.0

button.isEnabled = false

回答by roberthuttinger

there are also the states:

还有以下国家:

   button.highlighted = NO;
   button.selected = NO;

回答by Zouhair Sassi

Objective C:

目标 C:

button.selected = Yes;
button.highlighted = NO;
button.enabled = Yes;

Swift 4:

斯威夫特 4:

button.isSelected = true 
button.isEnabled = true

Also you can use:(swift 4)

你也可以使用:(swift 4)

 if (button.state == .selected) {
   //do something
 }

回答by Birju

You can manually set state of UIButton.

您可以手动设置 UIButton 的状态。

UIButton *btnCheck=[UIButton buttonWithType:UIButtonTypeCustom];

if(btncheck isselected])
{
    btncheck.selected=FALSE;
}
else
{
    btncheck.selected=TRUE;
}

You can do operation on UIButton as per your requirement like perform some action when UIButton is selected and while not selected.

您可以根据您的要求对 UIButton 进行操作,例如在选择 UIButton 和未选择 UIButton 时执行某些操作。

Hope this will help you....

希望能帮到你....

回答by Firas Shrourou

for Swift 3 you can use

对于 Swift 3,您可以使用

button.isSelected = true

button.isSelected = true

回答by Lorne K

For anyone arriving here looking to change the 'state' of the button (as opposed to 'enabled'). Stephen is correct "This attribute is read only—there is no corresponding setter method."

对于到达这里希望更改按钮“状态”(而不是“启用”)的任何人。Stephen 是正确的“这个属性是只读的——没有相应的 setter 方法。”

What you really want to set is the state of the buttons cell.

您真正想要设置的是按钮单元格的状态。

[[myNSButtonOutlet cell] setState: NSOnState];  //Options NSOnState, NSOffState, NSMixedState