Calling method set as target on UIButton - iphone

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];

Related

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.

iPhone add event for a button in different class

I have a view controller let's call it "View Controller A".
I have a different class(derived from NSObject and in .h and .m pair), let's call this class "B".
From a function of "B" I am adding a button in "A" using addSubView.
The button is getting added but now I want to attach an event to this newly button.
I am using
[newButton addTarget:self etc. etc.]
but it isn't working.
I don't want to declare the event in View Controller "A".
Is there any way to get rid of this?
Thanks all for reading this..
[newButton addTarget:viewControllerA etc. etc.]
EDIT: More complete version:
[newButton addTarget:viewControllerA action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
This assumes that class B has a reference to View Controller A named viewControllerA. It also assumes that View Controller A has implemented the method:
- (void)buttonAction:(id)sender;
EDIT 2: So this is what you want:
[newButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
I assume you already have that and I assume that the reason it doesn't work is because you have a method that looks something like this:
+ (void)addButtonToController:(UIViewController *)controller;
That's a class method. There is no self in a class method. One possible solution is to make it a singleton class and simply change the method to:
- (void)addButtonToController:(UIViewController *)controller;
You would call that method using:
[[ClassB sharedInstance] addButtonToController:controller];
If you don't know how to create a singleton class I could update my answer a third time to include that. :)
EDIT 3: I still think my original answer is the correct one. You don't have to implement - (void)buttonAction:(id)sender; in every controller. You could use inheritance or a category, to have access to this method in every controller without having to implement it for every controller. If you need help with this let me know.

Simple UIButton Question

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.

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