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;
Related
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.
I have a UITableView that i would like to hide until the user taps the button searchButtonTapped. (I'm also using this button as an IBAction.)
Originally i'm hiding the table view as you see in the viewDidLoad, and i wanna show it after the button was tapped, but it does not shown up after i tap the search button. Do i missed something? For me, it seems it should be work properly, after the button was tapped i refresh the table view.
my .h file
#property (weak, nonatomic) IBOutlet UIButton *searchButtonTapped;
- (IBAction)searchButton:(id)sender;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.hidden = YES;
}
- (void)buttonTapped:(id)sender {
if (sender == self.searchButtonTapped) {
self.tableView.hidden = NO;
[self.tableView reloadData];
}
}
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
It's impossible to tell from the little bit of code that you posted. Add NSLog statements in your buttonTapped method that show entering the method, entering the if statement, the value of searchButtonTapped, and the value of self.tableView.
Then you can tell if the method is getting called, if the if statement is evaluating as true, and if the table view is non-nil. One of those things is likely to be the cause of your problem.
I'm guessing that the if statement is wrong. what type is the property self.searchButtonTapped? Post the code that declares that property.
Based on the name I would guess that searchButtonTapped is a boolean?
you have declared only one IBAction, which is for the method searchButton.
This method call the searchSetup´s method. What is the purpose of it?
- (IBAction)searchButton:(id)sender {
[self searchSetup];
}
So you must have another IBAction for buttonTapped method witch is currently a "void" method and not a IBAction. Or you make that connection from the storyBoard, or you must declare it programaticly like:
[self.searchButtonTapped addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]
I have a record button, which when pressed, I want to hide the instructions button.
Here's the code for the record button:
// Create custom overlay
// Create instruction/record button
// Add instruction/record button to custom overlay
[_videoRecordBtn addTarget:self action:#selector(startVideoRecord:) forControlEvents:UIControlEventTouchUpInside];
So in startVideoRecord I should have something like:
-(IBAction)startVideoRecord:(id)sender{
[_instru setHidden:YES];
// start recording...
}
But I have no idea how to pass the _instru button over to startVideoRecord.
Add a property to your ViewController to keep a reference to your instructionsButton:
#property (nonatomic, strong) UIButton *instructionsButton;
When you create your instructionsButton, assign it to this property.
Then you can access the button via this property anywhere in your ViewController with self. instructionsButton.
So, your action method would be like:
-(IBAction)startVideoRecord:(id)sender{
self.instructionsButton.hidden = YES;
// start recording...
}
You can do this by 2 way..
1 way - > you set the tag of instructions button.
and use this
-(IBAction)startVideoRecord:(id)sender{
UIButton *instruBtn = (UIButton*)[self.view viewWithTag:your button tag];
instruBtn.hidden = YES;
// start recording...
}
2nd Way - > you make property for your instructions button and use like this
-(IBAction)startVideoRecord:(id)sender{
self.instruBtn.hidden = YES;
// start recording...
}
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
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";
}
}