Connect touch event in ios - iphone

Now that I know that NSEvent does not exist on ios - what do I need to do to capture an event like touchUpInside and cause it to call my method to handle it, WITHOUT Interface Builder?
I think this like asking how do I link an event to an outlet without IB...
I know it can be done, but I can't find anything that shows how - except Mac-only examples that use NSEvent.

Take a look at this method in UIControl:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Each of the different event types has a constant, such as UIControlEventTouchUpInside. Here's an example of it being used:
[addButton addTarget:self action:#selector(increment:) forControlEvents:UIControlEventTouchDown];
Look here for the values of controlEvents, listed in the Constants section.

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.

use two different control-event on UIButton

I have created one dynamic button in my application and I call the following method:
[btnactions addTarget:self
action:#selector(deleteview:)
forControlEvents:UIControlEventTouchDown];</b>
This deleteview method is used for delete the specific application view whenever I click the button.
Now, I want another method to call dynamically on button event
UIControlEventTouchDownRepeat
So how can I call two different events using different control events?
You can call addTarget again with UIControlEventTouchDownRepeat
From the docs (UIButton inherits from UIControl):
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
You may call this method multiple times, and you may specify multiple
target-action pairs for a particular event. The action message may
optionally include the sender and the event as parameters, in that
order.

Xcode - Equivalent to Actionscript 3 event listener

Is there a similar method in xcode to the AS3 "AddEventListener" code?
I want to be able to watch for a certain thing to happen, but not use up too much memory.
Basically I have 8 buttons. Obviously I can't just go through a for loop to see if a touch is on them, I need an event or a trigger or something.
(The reason I don't just use normal buttons is that I need to be able to slide onto them.)
Any ideas?
I assume you are implying you are using UIView and not UIButton. What you are looking for is a UIGestureRecognizer, which you would attach to the view. Review the SimpleGestureRecognizers sample project for examples of how to accomplish this.
[yourButton addTarget:self action:#selector(clickHandler:) forControlEvents:UIControlEventTouchUpInside];
-(void)clickHandler:(id)sender{
//your actions
}

UiTextField events

When I create UITextField inside Interface Builder, I can access Events tab for it, which has events like Value changed, Touch cancel, Touch drag, etc. I can assign my own methods to every of those events. How can I do the same, when I create UITextField programmatically with alloc?
Refer to Apple documentation for UIControl. After initializing your textField, call addTarget:action:forControlEvents:
example for the touch event ending an edit session
[textField addTarget:self action:#selector(handleTouchValueChanged:) forControlEvents: UIControlEventEditingDidEnd]
Instead of UIControlEventValueChanged, you should use UIControlEventEditingChanged:
[_titleTextField addTarget:self action:#selector(handleTitleValueChanged:) forControlEvents:UIControlEventEditingChanged];
UIControlEventEditingChanged fires whenever user changes value [synchronous with typing or keyup]
which could cause extra hits to your data handler routine, if you're saving values based on that event, expecting it to be some kind of final value from user input...

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.