Disable UIButton programmatically - ios

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)

It seems ok to me. Are you synthesizing the button? Try
self.myBtn.enabled = NO;

If you're looking for Swift3 solution
var myBtn = UIButton()
myBtn.isEnabled = true

You should be setting up your button as a IBOutlet.
#property (weak, nonatomic) IBOutlet UIButton *myBtn;
That way you can connect that button in storyboards. Which could be your issue.
Then call this to disable.
[_myBtn setEnabled:NO];

Do try this ..
In .h :
#property(nonatomic,retain)UIButton *myBtn;
In .m :
#synthesize myBtn;
And then,replace your [myBtn setEnabled: NO]; code with [self.myBtn setEnabled: NO]; code.

If you are looking for swift code:
var button = UIButton()
button.enabled = false //disable the button

Related

ios - Error when hiding button through code Objective-c

I'm trying to hide a button through code, but the program crashes every time I try to hide it.
The error I get:
'NSInvalidArgumentException', reason: '-[UIBarButtonItem setHidden:]: unrecognized selector sent to instance 0x14ef8f30'
.h file code:
#property (strong) UIButton *takeAll;
.m code:
#synthesize takeAll;
// function
[self.takeAll setHidden:YES];
Error is clearly saying 'You are trying to set hidden value of UIBarButtonItem
You created a UIButton object and allocation as UIBarButtonItem. This is wrong. It suppose to be
self.takeAll = [UIButton buttonWithType:UIButtonTypeCustom]
If you need UIBarButtonItem then
#property (strong) UIBarButtonItem *takeAll;
And if you want hide UIBarButtonItem.
self.takeAll.enabled = false
self.takeAll.tintColor = UIColor.clearColor
Enable the bar button item
self.takeAll.enabled = true
self.takeAll.tintColor = UIColor.blueColor
UIBarButtonItem does not have a setHidden: selector. You may want to set the tint color of the button to a clear color and disable it, which will essentially provide the same functionality.
If you have subclass of UIView inside UIBarButtonItem you can hide it with this code`
[barButtonItem.customView setHidden:YES];
The crash shown is in UIBarButtonItem.
In the interface file the declaration is for a UIButton. May be you are trying to link a UIButton to UIBarButtonItem.

Changing button text of different button on button click

I have 3 buttons in my storyboard and ViewController that are working as expected:
- (IBAction)button0:(id)sender {
[sender setTitle:#"btn0 pressed" forState:UIControlStateNormal];
}
- (IBAction)button1:(id)sender {
[sender setTitle:#"btn1 pressed" forState:UIControlStateNormal];
}
- (IBAction)button2:(id)sender {
[sender setTitle:#"btn2 pressed" forState:UIControlStateNormal];
}
I have a fourth button that, when pressed, I would like to change the displayed text of button0-2 to an empty string.
- (IBAction)resetAllButtons:(id)sender {
//In Android this code would be something like:
//Button btn0 = (Button) findViewById(R.id.button0);
//btn0.setText(" ");
}
How do I do this? I've found many ways to change the button text, but only of the current button being pressed. Can I target all the buttons by id somehow?
Figured it out (although still not clear on why it works?)
I connected button0 as an IBOutlet in my ViewController.h file.
#property (weak, nonatomic) IBOutlet UIButton *button0;
From there I was able to reference it in my ViewController.m file using
[self.button0 setTitle:#" " forState:UIControlStateNormal];
But why am I able to do that? I thought that if I declared
- (IBAction)button0:(id)sender;
in my ViewControler.h file that I couldn't also have an outlet connected to the same object? Thanks for reading either way.

Multiple clicks on UIButton trigger Target function multiple times

I have a UIButton. I bound a target as follows.
[button addTarget:self action:#selector(myFunction)
forControlEvents:UIControlEventTouchUpInside];
When i click my Button multiple times quickly it invoke the target function multiple times.
On Tapping button i present a new View controller.
when i click 3 times quickly then my new view controller is shown 3 times.
This is something stupid. Whats the point of triggering the function again once the View has been shifted to a new View controller. Why the Hell Apple do such stupid things ?
Any Help please?
First of all its not apple bugs. It should be handle manually. So follow these step
First make your global instance of your button then do this
.h file
#property (weak, nonatomic) IBOutlet UIButton *btn;
.m file
- (IBAction)myFunction:(id)sender
{
self.btn.userInteractionEnabled = NO;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.btn.userInteractionEnabled = YES;
}
Take one global bool flag like "isItDone" or it declare in singleton class.
in "myFunction" set it as false
which View controller you push on that function in that class's "ViewDidAppear" method set as true.
it will help you.
I have same problem and it is good solution for that to manage it using one global variable.
I think this will help you.
Change your calling function like this
- (IBAction)myFunction:(id)sender
{
UIButton *button = (UIButton*)sender;
button.userInteractionEnabled = NO;
}
and call your function like this
[button addTarget:self action:#selector(myFunction:)
forControlEvents:UIControlEventTouchUpInside];
if you want to store the selection incase you came back to the view controller then only you need to keep a boolean flag to store if its clicked once or not.
Set the IBOutlet to your button, in the viewWillAppear method write,
button.userInteractionEnabled = YES;
and when you click on the button set,
button.userInteractionEnabled = NO;

Unhiding a UIView when button is clicked

I've hidden a UIView using
_loginview.hidden = YES
and when I do
-(IBaction)logInButton:(id)sender {
_logInView.hidden = NO;
}
It still doesn't show when I click the button, can anyone help?
I have created a Iboutlet property of UIView and connected it with UIView in storyboard. Also ticked hidden.
Screen shot of hidden view is below(color orange). I also sethidden in programmatically way and still working.
And later on button action I perform setHiden as no and it appeared. Code of IBAction is below:-
- (IBAction)loginBtn{
[hidenView setHidden:NO];
}
You need to setHidden NO on your View when you clocked on uiButton..
(IBaction)logInButton:(id)sender{
[_logInView setHidden:NO];
}
It is working my end by creating this,
#property(nonatomic,strong) IBOutlet UIView *viewLogin; // in controller.h
#synthesize viewLogin; // in controller.m
set outlet property for viewlogin in storyboard
and with following IBAction event,
-(IBAction)login:(id)sender{
[viewLogin setHidden:NO]; }
In storyboard, from connections inspector check that your button is connected properly with a sent event.
Then (if you are performing an async process, maybe login user) try this:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_logInView.hidden = NO;
}];
witch updates the UI from the main thread.
Just a guess.
Better use setAlpha instead of setHidden.

Switches in ios

I am a mere beginner in iOS. I have seen this statement ** [leftSwitch setOn:setting animated:YES];** in a tutorial describing about switches.
UISwitch *leftSwitch;
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
[leftSwitch setOn:setting animated:YES];
Please explain me the need of keyword animated in this context.Thanks in advance
in objC, parameters dont come behind the method name like in C or java or ...
in objC, parameters are interwoven with the method name..
lalalaa:PARAM lalala:PARAM2 lalala:PARAM3
after every : comes a ONE param
so animated: = part of method name
setOn:animated:
The animated part of that method determines where the changing of that switch state is a nice smooth animation or not. Try both YES and NO and you will see.
setOn:animated:
Set the state of the switch to On or Off, optionally animating the transition.
(void)setOn:(BOOL)on animated:(BOOL)animated
Parameters
on
YES if the switch should be turned to the On position; NO if it should be turned to the Off position. If the switch is already in the designated position, nothing happens.
animated
YES to animate the “flipping” of the switch; otherwise NO.
SEE: http://developer.apple.com/library/ios/#documentation/uikit/reference/UISwitch_Class/Reference/Reference.html
Drag and drop UISwitch in xib file.
In .h file:
IBOutlet UISwitch *onoff;
In .m file:
[onoff addTarget:self action:#selector(switchTapped:) forControlEvents:UIControlEventValueChanged];
// Switch
-(void) switchTapped: (id) sender {
UISwitch *switchControl = (UISwitch*) sender;
BOOL value = switchControl.isOn;
if (value) {
lbl4.text=#"ON";
}else{
lbl4.text=#"OFF";
}
}

Resources