How can I determine whether my button's event is Touch Down?
I want to do a function like this:
if(users click on touchdown event)
{
NSLog(#"a");
}
else if(users click on touchupinside event)
{
NSLog(#"b");
}
Eather you set two different IBAction methods in the InterfaceBuilder or you set two different targets via:
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
in your code while creating the button.
You "find out" by letting the button tell you when the event happens.
Add a method (or methods) like this:
- (IBAction)myButtonClick:(id)sender;
In Interface Builder, attach the method(s) to the events you're interested in.
You create a separate method for each type of event if you want different behavior for a TouchDown as opposed to TouchUpInside.
you attach each unique event to its own IBAction
Related
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
I have a button, when held down motion occurs, when released it should stop. I know I could write two methods, one to start and one to stop motion, I was wondering if in the one method I could detect both based on Touch Up Outside vs. Touch Up Inside.
If Touch Up Outside do this if Inside do this.
-(IBAction) startMotion: (id)sender {
NSLog(#"Forward or back button is being pressed.");
UIButton * buttonName = (UIButton *) sender;
NSLog(#"Button Name: %#", buttonName.currentTitle);
}
Yes, you could write one method and link the IBAction "touchUpInside" and "touchUpOutside" to the same IBAction member function - via Interface Builder.
The only downside would be that you would not be able to tell if it was a touchUp inside or outside when it was called. (It doesn't sound like this matters to you.)
If the later was an issue, you could alternately implement independent touchUpInside and touchUpOutside methods which in turn call the same method - a third custom one - into which you could pass an "inside" or "outside" flag from the proper IBAction handler.
Touch up Outside won't give you the functionality you're looking for, I don't think. The person would have to move their finger outside the boundaries of the button and then lift remove the finger
Why not use touchdown to initiate the motion and touch up inside to stop it?
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? 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.
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...