Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable UIButton programmatically

I'm stumped at why this code isn't working. I have a login button that I want to disable when the user isn't logged in.

I have a UIButton delared in my .h file like so:

   IBOutlet UIButton *myBtn;

I've set up a referencing outlet in Interface Builder to this button.

In my .m file, I've tried:

[myBtn setEnabled: NO];

and

myBtn.enabled = NO;

But neither of these disable the button in the conditional statement I'm in. (I want to disable the login button when the user successfully logs in)

I'm able to do this with two other buttons on the same screen, so I know the code is correct. I don't throw any errors, so I think the object exists. The references to myBtn change color in XCode, too, so it appears to be a valid reference.

I must be missing something. What am I doing wrong here? (I'm a Windows developer, relatively new at Objective-C)

like image 978
Tim Avatar asked Oct 14 '13 14:10

Tim


People also ask

How do I disable UI button?

In this Swift code example, you will learn how to disable UIButton. The quickest way to disable the button in Swift is to simply set the button's isEnabled property to false. For example button. isEnabled = false.

How do I disable a button in swift 5?

To disable a button, you should use the disabled view modifier. It takes bool parameter, which for example, can be taken from the state. You can use the disabled modifier on a view above buttons ( VStack , for example). That way, you will apply it to all buttons (or other controls below).


2 Answers

It seems ok to me. Are you synthesizing the button? Try

self.myBtn.enabled = NO;
like image 184
ssantos Avatar answered Oct 25 '22 22:10

ssantos


If you're looking for Swift3 solution

var myBtn = UIButton()
myBtn.isEnabled = true
like image 28
xRab Avatar answered Oct 25 '22 22:10

xRab