UiTextField events - iphone

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...

Related

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.

UISlider problem - how to detect a user releasing the slider button in iOS?

I can detect the value change event from the UISlider, but now I need to detect a user releasing the slider button.
How to detect this event (or touch on slider ends) ?
Should I use the "touch up inside" event for detection ?
Thanks.
You can also use the UISlider's UIControlEventValueChanged. Set the continuous property to NO as to initiate when the user is done selecting a value.
[mySlider addTarget:self action:#selector(myMethod) forControlEvents:UIControlEventValueChanged];
mySlider.continuous = NO;
I've not worked with sliders, however as UISlider inherits from UIView - surely you can just tap into touchesBegan: and touchesEnded: to perform your task.
Through IB you can attach "Touch Up Inside" and "Touch Up Outside" to a method/action of the viewController for slider release.
Or more programmatically (probably in your viewController), you can use
- (void)addTarget:(id)target action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
to detect when the UIControlEventTouchUpOutside and UIControlEventTouchUpInside events occur to your UISlider.
References
method
control events
UIControlEventTouchDown event to detect when the user starts using the control. UIControlEventTouchUpInside when the user finishes using the slider. To add the event just do as #JohnK suggested

Button should be visible when the user is done selecting the value through slider

I need to implement such kind of functionality in my slider such that the submit button should not appear till the user pull up his fingers from slider.
Is there any kind of function which can duplicate this.
Thanks,
The UISlider has a continuous property. Set this to NO, and you will only receive valueChanged events when the user is done choosing his value. Then add a target for the valueChanged event, and there you can set the button to be enabled.
If you can't set continuous to NO for some reason, observe the UIControlEventTouchUpOutside and UIControlEventTouchUpInside events:
[yourSlider addTarget:self
action:#selector(showSubmit:)
forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
And
- (void)showSubmit:(UISlider *) {
// code to show your button here
}

Does UITextField consume touch events?

My view controller has UITextField object as a subview. The controller is set as target to handle the text field's UIControlEventTouchUpOutside event.
I'm trying to use this event handler for dismissing keyboard when the text field becomes first responder, but it seems to be never called. Delegate methods like textFieldShouldReturn work just fine.
Why the text field object doesn't send action message to the target? I tried this scheme for bunch of all touch events, but no luck.. Or do I have to subclass UITextField somehow to be able to catch this event?
Thanks in advance!
UITextField for sure responds to those event to handle cursor positioning and so on. It might not 'forward' those event to its parent. If you need to intercept those, you can subclass UITextEvent and catch those event yourself (of course do not forget to call [super blablabla] in order to keep the standard behavior.
Maybe It just doesn't respond the UIControlEventTouchUpOutside event. I am sure it will respond UIControlEventValueChanged event. I think you can put a transparent custom UIButtom as background view, and if any touchInSideUp in the UIButtom, you can dismissing keyboard as desired.
It responds to the same events as UIButton. Take a look in IB. Also you may consider using UITextFieldDelegate method
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField

How to I respond to a user pressing a UISegment?

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.