ios 如何禁用 UIButton?

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

How do I disable a UIButton?

iosobjective-ccocoa-touchuibutton

提问by Rahul

I am working on a project in which I have to show all photos of Photo Library in a plist and show them horizontally on UIButtons.

我正在做一个项目,我必须在 plist 中显示照片库的所有照片,并在UIButtons 上水平显示它们。

My app will also have an edit button: when the user clicks this button, a delete mark (such as usually appears in other iPhone/iPad apps) should show on each button.

我的应用程序还将有一个编辑按钮:当用户单击此按钮时,每个按钮上应显示一个删除标记(例如通常出现在其他 iPhone/iPad 应用程序中)。

But here's the crucial bit: as soon as this delete mark appears, the functionality of the button should be disabled. I've tried accomplishing this with the following:

但这里是关键的一点:只要这个删除标记出现,按钮的功能就应该被禁用。我尝试使用以下方法完成此操作:

{
  editbutton.enabled=NO;
}

...but it neither produces an error nor works. What should I do?

...但它既不会产生错误也不会起作用。我该怎么办?

回答by Mehul Mistri

Please set this...

请设置这个...

 editButton.userInteractionEnabled = NO; 

or You can use

或者你可以使用

 editButton.enabled = NO;

回答by Jhaliya

Use the enabledproperty of UIControlwhich the super class of UIButtonand set it with NO.

使用超类的enabled属性并将其设置为 NO。UIControlUIButton

myButton.enabled = NO;

myButton.enabled = NO;

You could also try as @Marvinsuggested, In that case your button will not respond to any touch event from user,

您也可以按照@Marvin 的建议尝试,在这种情况下,您的按钮将不会响应用户的任何触摸事件,

回答by spjatkin

setter for property enabledis overridden in class UIButton.try to send a message.

启用属性的设置器在类UIButton 中被覆盖尝试发送消息。

 [editbutton setEnabled:NO];

回答by Schemetrical

Swift 3 and above

Swift 3 及以上

editButton.isEnabled = false

editButton.isEnabled = false

setEnabledis now part of the setter method for isEnabled.

setEnabled现在是设置器方法的一部分isEnabled

回答by Patrick Domegan

In addition to the above:

除上述之外:

Set editButton.userInteractionEnabled = NO;or you can use editButton.enabled = NO;

设置 editButton.userInteractionEnabled = NO;或您可以使用 editButton.enabled = NO;

You might want to gray out the button so the user knows it was disabled: editButton.alpha = 0.66

您可能希望将按钮灰显,以便用户知道它已被禁用: editButton.alpha = 0.66

回答by Pradumna Patil

yourBtn.userInteractionEnabled = NO;