在 iOS 中禁用按钮

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

Disabling a button in iOS

iosuibutton

提问by lakesh

I need to disable a button if the key does not exist in the dictionary. I have used the setEnabled functionality of the UIButton but the image that has been set default still appears.

如果字典中不存在键,我需要禁用一个按钮。我已经使用了 UIButton 的 setEnabled 功能,但已设置为默认值的图像仍然出现。

The code looks like this:

代码如下所示:

if([self.InfoDictionary objectForKey:ButtonExist])
{
    [button1 setEnabled:YES];
}
else
{
    [button1 setEnabled:NO];
}

The image still appears when I run in the simulator. Need some guidance on this.

当我在模拟器中运行时,图像仍然出现。需要一些指导。

回答by Vinayak Kini

enable = YESproperty of button performs the action when clicked.

enable = YES按钮的属性在单击时执行操作。

enable = NOproperty prevents action to be executed on click.

enable = NO属性阻止在单击时执行操作。

If you want to hide the button then you can set the hiddenproperty as YESor vice versa. Other way to hide is by setting the alphaproperty to 0(invisible) or 1(visible)

如果要隐藏按钮,则可以将hidden属性设置为asYES或反之亦然。其他隐藏方法是将alpha属性设置为0(invisible) 或1(visible)

回答by iPatel

Also you can set userInteractionEnabledproperty of UIButton

你也可以设置userInteractionEnabled属性UIButton

 if([self.InfoDictionary objectForKey:ButtonExist])
    {
        [button1 setEnabled:YES];
        button1.userInteractionEnabled = YES;
    }
    else
    {
        [button1 setEnabled:NO];
        button1.userInteractionEnabled = NO;
    }

回答by Yashesh

use :

用 :

if([self.InfoDictionary objectForKey:ButtonExist])
{
    [button1 setHidden:YES];
}
else
{
    [button1 setHidden:NO];
}

if you want to hide UIImage of UIButton then:

如果你想隐藏 UIButton 的 UIImage 则:

if([self.InfoDictionary objectForKey:ButtonExist])
    {
        [button1 setBackgroundImage:[UIImage imageNamed:@"YOUR IMAGE"] forState:UIControlStateNormal];
    }
    else
    {
        [button1 setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }

Hope this will help you. All the best !!!

希望这会帮助你。祝一切顺利 !!!

回答by Jonghee Park

in swift3:

在swift3中:

self.button.isEnabled = false

回答by Seamus

Ran into this myself. The problem was that I was inadvertently enabling the button in a gesture handler for tap!

我自己遇到了这个。问题是我无意中启用了手势处理程序中的按钮以进行点击!

Look for side effects like this.

寻找这样的副作用。