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.
Related
I am developing an application in which I want to select the button with some given tag. For example tag=12. So, what I want is that when the button with tag 12 is selected the button clicked method also gets called.
One more thing I want to ask, if I write
button.selected=YES
will the button method automatically get called? If not then how to call the button method from somewhere else where I do not have sender (button properties) value?
The only thing I have is the button tag.
Please help and ask me for any clarification.
Create a temporary UIButton and give the tag of button you want to call.
For eg. call button action method with temporary button of tag 12
UIButton *button = [[UIButton alloc] init];
button.tag = 12;
[self buttonTapped:button];
Hope it helps. Comment down for any query.
If you're setting button's property to Selected manually , then for it's click event you will have to call it manually , When you set button Selected like :
[self buttonCick];
iUser is close. You'll want to call the method you've linked to your button manually.
[self buttonClick:nil]
will work, if you're calling the buttonClick method from an object of the same class that contains the buttonClick method. Otherwise, you'll need to keep a reference to the object (perhaps a controller) containing the buttonClick method and use that instead of self.
[self.controller buttonClick:nil];
I have a button, and I'd like to call a function when the user pushes it then call a different function when he releases it.
Right now I'm using this to create the button:
[ScanButton addTarget:self action:#selector(scanButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
to call scanButtonPressed. As of now scanButtonPressed is only called upon release. How do I change this?
Thanks
How about by trying a control event other than UIControlEventTouchUpInside?
Try - UIControlEventTouchDown .
This contains all of the different events you can pass to a button
UIControl Class Reference. Try looking at these; both UIControlEventTouchUpInside and UIControlEventTouchDown will work.
I have 1 button and 2 methods. One method calls this:
[button addTarget:self action:#selector(action1) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
And the other calls this:
[button addTarget:self action:#selector(action2) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
For some reason, the button's action will not change. I am sure I am calling the above code correctly. Is it trying to add an action to the button and making it call both functions? If so, how can I stop this from happening? I have tried releasing and setting the button to nil before setting the new action and no luck. Thanks for the help.
Yes. It's actually calling action1 and action2. You need to call removeTarget before adding the new target on the button.
How to I respond to a user pressing a UISegment? Is there a delegate, or must I programmatically (or Interface Builder), attach the selectors?
If you prefer to do it in code you can use:
[segmentedControl addTarget:self action:#selector(didSelectIndex:) forControlEvents:UIControlEventValueChanged];
And this is then the method that would be called
- (void) didSelectIndex: (id) sender
{
NSLog(#"%#",[(UISegmentedControl *)sender titleForSegmentAtIndex:[(UISegmentedControl *)sender selectedSegmentIndex]]); //replace this with your code
}
If you prefer to use IB, right click on your UISegmentedControl select Value Changedand then attach it to the desired method in your first responder.
UISegmentedControl subclasses UIControl and sends out the control event UIControlEventValueChanged whenever the selected segment changes. You can add a target/action pair for this event in code, or you can do it with the normal control-click-and-drag in IB.
Now I am creating a IPhone App. I need to create multiple buttons dynamically and handle their corresponding events separately. Can each button events behave separately, or can I send a variable or an object to a method and depending upon that value differentiate the button behavior. Can anybody point me to how to do that?
Please Help,
Syam
You should check out the UI Catalog sample in the iPhone SDK. It has examples of programmaticaly creating all the UI elements.
I don't believe you can pass an argument to the function that's called when a button is triggered, so you would need a separate function for each button. However these could be lightweight functions that call another with a param.
To manually create a button and set its action you would do something like:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 170, 150.0, 30.0);
[button setTitle:#"My Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
When a button calls an IBAction (which would be the selector you pass in), it also passes a pointer to the button being presed in the :(id)sender argument you normally have:
- (IBAction) doSoemthing:(id)sender
So you can just figure out based on the button passed in, what you want to do. Either by creating a map based on all of the button addresses, or by having all UIButtons you use be subclasses of UIButton where you insert some kind of custom ID and look that up when doSomething: is hit.
You can use the tag property of UIButton programmatically and access the tag. This is easier than subclassing.
Here is an example:
Detecting which UIButton was pressed in a UITableView