ios - Error when hiding button through code Objective-c - ios

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.

Related

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;

Perform a click to other buttons objective c

So how can i make a button click other buttons in objective c for iOS? I use xcode.
IBOutlet UIBarButtonItem *save1;
IBOutlet UIBarButtonItem *save2;
IBOutlet UIBarButtonItem *save3;
IBOutlet UIBarButtonItem *save4;
These are the buttons, I want that button "save4" click button "save1", "save2" & "save3".
I understand the actions and outlets and I have that all connected, but I just wan't the code to click other buttons, the problem I have is that those buttons are on another view and I tried many code but they don't work. Here is a download link: Download
I think you are confused between "Outlets" and "Actions". For handling button clicks, you must define "Actions" for your buttons. For eg, write the following code in your view controllers .m file:
- (IBAction) saveOnePressed:(id)sender{
// Button one is pressed. Do your actions here.
}
And connect your button to this action.
EDIT:
As per rmaddys suggestion:
You can define the Action for your fourth button as the following:
- (IBAction)saveFourPressed:(id)sender{
// Here you can call all the other 3 actions
[self saveOnePressed:nil];
[self saveTwoPressed:nil];
//......
}

How to set an action for an UIBarButtonItem?

I try to set an action for an UIBarButtonItem like I did before with a normal button:
Select the UIBarButtonItem in storyboard.
Right-Click and pull to my .h-file
Change to action and choose UIBarButtonItem and give the action a name.
- (IBAction)haupt:(UIBarButtonItem *)sender;
Write some code in the function at my .m-file.
- (IBAction)haupt:(UIBarButtonItem *)sender {
NSLog(#"button");
}
Now I try this in the simulator, but nothing happens (No output on the console). What I am doing wrong?
Select the UIBarButton , Create an IBAction by CNRL+Dragging at the implementation section in .m file then try to print with NSLog use breakpoint to know if the control is reaching the IBAction you created.
Here is how you do it with Swift 3:
let check: UIBarButtonItem = UIBarButtonItem()
check.title = "Check"
check.width = CGFloat(100)
check.tintColor = UIColor.green
check.action = #selector(ViewController.checkTapped)
check.target = self
func checkTapped (sender:UIBarButtonItem) {
print("check pressed")
}
myBarButtonItem.target = self;
myBarButtonItem.action = #selector( myMethod: );
Remember action method must have the following signature:
- ( IBAction )myMethod: ( id )sender;
or refer this link : Add a custom selector to a UIBarButtonItem
Swift 4:
If you have already create it you can use the customView property:
barButton.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(barButtonTap)))
You need to set an Action:
[self.myButton setAction:#selector(haupt:)];
Or if you are using Storyboard. Right click und pull to you .m file (not .h)
You can use belowed code it is working
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]init];
barButton.title=#"Back";
[barButton setTarget:self];
[barButton setAction:#selector(uiBarBurronAction:)];
Button Action
-(IBAction)uiBarBurronAction:(id)sender
{
NSLog(#"barbutton Pressed")
}
Otherwise you can set selector
RightClick on uibarbuttonitem and drag selector and give action to viewcontroller
You ctrl-drag the button to you header file and create an IBOutlet.
Then you wire the button with the IBAction, all good.

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

Resources