简体   繁体   中英

Disabling a button in iOS

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.

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.

enable = YES property of button performs the action when clicked.

enable = NO property prevents action to be executed on click.

If you want to hide the button then you can set the hidden property as YES or vice versa. Other way to hide is by setting the alpha property to 0 (invisible) or 1 (visible)

Also you can set userInteractionEnabled property of UIButton

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

use :

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

if you want to hide UIImage of UIButton then:

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 !!!

在swift3中:

self.button.isEnabled = false

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM