Simple UIButton Question - iphone

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.

Related

addTarget:self versus addTarget:nil

I am new in iOS development. I am little confuse when I am adding a button programmatically.When we assign a target to button like:
[button addTarget:self action:#selector(CallMe) forControlEvents:UIControlEventTouchUpInside];
and
[button addTarget:nil action:#selector(CallMe) forControlEvents:UIControlEventTouchUpInside];
It is calling CallMe method in both the cases. Can anybody explain me what is the actual difference between these two lines of code.It will more helpful if anyone can explain the working of addTarget specially.Thank you very much. Help would be appropriated.
If you add self or any other object as the target for an action message the message will be sent to exactly this object.
Adding nil as a target means that the actual target will be searched at runtime when the message is triggered. The lookup starts at the first responder object and continuous from there along the responder chain, that is by trying the object returned by the nextResponder method until an object is found that implements this method. Take a look at the event handling guide for more information on the exact lookup order.
According to Apple's documentation,
The target object is the parameter send to addTarget method—that is, the object to which the action message is sent. If this is nil, the responder chain is searched for an object willing to respond to the action message.
If you want to remove the action, you can pass nil to remove all targets paired with action and the specified control events on the remove target method,
[button removeTarget:nil action:#selector(CallMe) forControlEvents:UIControlEventTouchUpInside];
Here is description of parameter Target from apple's documentation for UIControl class:
target
The target object—that is, the object to which the action message is sent. If this is nil, the responder chain is searched for an object willing to respond to the action message.

Button down/up in objective c for iphone?

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.

Calling method set as target on UIButton

If I have a UIButton which has a target set, is there a way to call this method without explicitly calling it?
For example:
[newViewController.button addTarget:self action:#selector(MyMethod) forControlEvents:UIControlEventTouchUpInside];
I'm then looking to call this method in newViewController but cannot as MyMethod belongs to the parent view. So I really want to be able to say, just call the method/target on the UIButton button. But how can I do this?
Thanks
Just found out how to do this. Was pretty easy, feel like a noob asking now!
[button sendActionsForControlEvents:UIControlEventTouchUpInside];

Calling a method which is also used by UIButton?

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.

How to create multiple buttons dynamically and handle their separate events? + iPhone SDK

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