Button down/up in objective c for iphone? - 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.

Related

how to pass data through #selector upon custom button click?

I'm making a button via code.
I have the following line of code to trigger a method when the button is clicked:
[imagesButton addTarget:self action:#selector(photoClicked:) forControlEvents:UIControlEventTouchDown ];
The problem is that I can't pass data to the method through the #selector;
If the button had a background image name "background.png" how would I go about sending the name of the background image to the method when the button is clicked?
This all HAS to be through code.
Thanks!
-Shredder
there must be a way to comment on an answer, but I don't know what it is. Anyway, Gobot above me forgot to write (id) before sender in the method declaration. Otherwise Gobot's example is ok.
Well if you're trying to change a property of the button whose sending the message, your selector should have a parameter of sender, which is a pointer to the object that called it, which is your button in this case. For example:
- (void)photoClicked:(id)sender {
UIImage bg = [sender currentBackgroundImage]
}

How to trigger UIControlEventTouchCancel?

I thought UIControlEventTouchCancel is triggered when I tap on a button and then drag out of it. But I'm doing exactly that and my event handler is never called.
I have a class that inherits from UIControl and I'm doing this in the init:
[self addTarget:self action:#selector(onTouchCancel)forControlEvents:UIControlEventTouchCancel];
How do I trigger the cancel event?
Thanks for your help!
Use "UIControlEventTouchUpOutside" if you want an event when the finger is outside the bounds of the control.

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.

Need something other than touchup inside

I have a button in my iphone app, when I click the button it calls a linked method, that I have done as:
[myButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
all working well, but i want if user tap the button for say 5 second continuously then he will be shown a message that will be there and gone away after some time and button does not get clicked, how can I do that, please help me.
Many Thanks in advance.
Regards
iPhone Developer11
Youcould use UIControlEventTouchDown and make sure it is touched down in the right view and if it is start a timer. If there are no UIControlEventTouchUpInside for 5 seconds you do whatever you need to do.
You should check docs for UILongPressGestureRecognizer. I think it fits your requirements.

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.