In my project I have a button that calls a function via a IBOutlet, I need to know how can I put in the function code to make disable the button, as I identify the button (using tags?)
And what to write?
I think you're slightly confused. Your button is connected to a method via an IBAction. You'll need to create a #property for your button and connect it to IB via an IBOutlet.
Alternatively you could utilize the sender argument from the IBAction. Something like this perhaps...
- (IBAction)buttonPressed:(id)sender
{
UIButton *buttonThatWasPressed = (UIButton *)sender;
buttonThatWasPressed.enabled = NO;
}
Very simply.
self.yourButton.enabled = NO;
For that make code like this
button.enabled=NO;
& if you want to enable it agaian code like this
button.enabled=YES;
Related
I need several UIButtons to all connect to the same IBAction so the buttons can all do the same thing without having to copy and paste the code. Please tell me if there is a way to do this! It might be right under my nose, but I can't find it. Thanks!
Simply add the same target and selector to each button:
[button1 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
// etc.
For this You need to use set IBOutlet for Each Button or Set tag for each button if you are using Outlet then used this code .h
#interface RootViewController_Phone : UIViewController
{
IBOutlet UIButton *btn1;
IBOutlet UIButton *btn2;
}
- (IBAction)buttonPressed:(id)sender;
-(void)CallButtonsMethod;
#end
Now in .m file
- (IBAction)buttonPressed:(id)sender{
if([sender isEqual:btn1])
{
[self CallButtonsMethod];
}
if([sender isEqual:btn2])
{
[self CallButtonsMethod];
}
}
-(void)CallButtonsMethod
{
//your Code
}
Use IBOutletCollections.
Here is tutorial for that:
http://useyourloaf.com/blog/2011/03/28/interface-builder-outlet-collections.html
I've found that I'm usually not able to hook up multiple buttons (or even a single button, for that matter) in IB to an already existing IBAction in code, by Ctrl-dragging from the button to the IBAction in the m file. Xcode tries to create a new action in the dragged-to file, but won't connect to an existing action. [This is true for Xcode 4.6.1 and several previous (maybe all) versions.]
The approach in the accepted answer in the following link works if the IBAction is in the view controller which is the "File Owner" for the view containing the button:
Connect object in .xib to existing IBAction
However if you want your IBAction to be in the view's own class, rather than the view controller's class, then the following approach works:
Drag only one button onto the canvas in IB first. Control-drag from
the button to the view's .m file.
Drop the dragged line in any vacant space in the implementation section of the code.
Define a new IBAction in the little popup dialogue box. So now you have one
button hooked up to an IBAction.
Now instead of dragging more buttons from the object library, hold
down the Option key and drag out a new button from the original one.
This new button will come hooked up to the same IBActions as the
original one. (If you need to create lots and lots of buttons and
this seems tedious, you can select a bunch of already created ones simultaneously and
Option-drag to create as many more in a single go.)
I am newbie at iOS programming. I want to put notifications on UIbutton (for example displaying the click count on top of its corner), shortly it informs some counting on UIButton.
Is it possible? Please give some ideas or some short code for how can I do it.
in the header file add
{
IBOutlet UIButton *counterbutton;
}
-(IBAction)pushed:(id)sender;
in the .m file
int count;
-(IBAction)pushed:(id)sender
{
count = count + 1;
counterbutton.text = [NSString stringWithFormat:#"count is %i",count];
}
In the interface builder connect the button to the view controller for both the IBAction and IBOutlet.
Flex_Addicted share that link in my question i guess sharing in here more discernible. In that example shows exactly what i asked and it is really easy do just add h and m files in your project. How can I add a badge to a standard UIButton?
in xxx.h
UIButton *b1, *b2, *b3;
in xxx.m
b1 = ---- similarly for b2 and b3
Now I want that on Click event I store the title in the string. How i can achieve it?
In Other Words:
What function/method would I have to implement in my View Controller class to handle a click event on a UIButton?
You can make an IBAction for touch up inside in Interface Builder. Then I am assuming you have a UITextField for the text.
Since your not using Interface Builder you need to:
-(void)getStringFromText:(id)sender {
NSString *input = sender.titleLabel;
}
//In some start up method.
[b1 addTarget: target action: #selector(getStringFromText:) forControlEvents: UIControlEventTouchUpInside];
When you declare your UIButtons, you need to put "IBOutlet" before them in your class definition - then you need to to use Interface Builder to link/reference the UIButtons to the ones you have declared in your code.
Once you have done this, you need to develop a (IBAction) method - and have it simply get the "text" property from the calling object. In Interface Builder reference this method for all the buttons for the "touch up" event.
If this isn't clear I will post up code as an example?
So i've created a custom TableViewCell, and in the nib I put a UISwitch. Before I had even hooked it up to anything, if I ran it in the simulator and clicked on it, it would switch from off to on with the animation and such.
I'm trying to add a feature where the user is only allowed to change from off to on when a certain condition is true. I want it so, when the user touches the switch, it checks if the condition is true, and if it isn't it the switch doesn't move.
I've set up an IBAction where if the user Touches Up Inside, it'll run my function. My function is this:
if([on_switch isOn])
{
if([my_switch canSwitchOn])
{
NSLog(#"SWITCHED ON SUCCESSFULLY");
[on_switch setOn:TRUE animated:TRUE];
}
else
{
NSLog(#"SWITCHED ON UNSUCCESSFULLY");
//Put in popup here
}
}
else
{
[[ClassesSingleton sharedSingleton] classSwitchedOff:cell_index];
[on_switch setOn:FALSE animated:TRUE];
}
However, no matter what I do, that switch will flip, even though I gave it no directions to do so. I'm pretty sure it's the auto-flip that cause it to do so even before I'd hooked anything up to it. Is there a way to turn that off?
Thanks!
What you need to do is set userInteractionEnabled property to False on your UISwitch.
If you had allready made the connection in Interface Builder to an IBOutlet you delared in your owning class, you would be able to set it in code like this:
mySwitch.userInteractionEnabled = NO;
You could also set the property directly in Interface Builder by selecting the checkbox, as shown below (However in your app, you are going to need to wire the button up to an IBOutlet anyway to implement your conditional logic.)
alt text http://www.clixtr.com/photo/ef06c1f7-8cca-40cd-a454-5ca534ceb9fe
I think you will need something like the following:
// Somewhere just after the creation of _switch
[_switch addTarget:self action:#selector(switchValueDidChange:) forControlEvents:UIControlEventValueChanged];
// Target/Action method
- (void)switchValueDidChange:(UISwitch *)sender {
if(sender.on && [self canSwitchOn] == NO){
[sender setOn:NO animated:YES];
// Popup
}
}
Problem you're having is that the switch has already committed it's on state on touch up. When that's not the case (I'm not sure, never tested) you have to check whether the switch is currently not on. This bit of code will revert the state when the user was not allowed to switch the switch.
A better way is to disable the control, but maybe that's not what you want in this case.
I have a small iPhone app which I've created a button with some functionality in. My question is, how can I call this button without actually pressing it?
Any help would be appreciated!
Thanks
If you want to activate whatever target a button is wired to, you can call:
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
(TouchUpInside is the event you'd normally wire a button action to). This way if other targets are added or changed for any button (say for debugging) you don't have to alter your code.
This method is on UIControl which UIButton inherits from, which is why you might have overlooked it at first glance...
Have your button event call a function. You can also manually call the function yourself.
Example:
- (void) btnFunction {
NSLog (#"test");
}
...
UIButton *btn1 = [UIButton buttonWithType:UIButtonRoundedRect];
// other code to set up button goes here
[btn1 addTarget:self action:#selector(btnFunction) forControlEvents:UIControlEventTouchUpInside];
You can also call the function yourself:
[self btnFunction];
Your button shouldn't have functionality, it should just send a message to its target (or call a method, or call a function...).
You're free to send that message to that target yourself.
e.g. Your button's target outlet is connected to an IBAction on your controller. That IBAction is just a method of the form:
- (void) doSomething:(id)sender
In your own code do:
[controller doSomething:self];
It's exactly the same as having your button do it.