Does UITextField consume touch events? - iphone

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

Related

what's the events of getting and losting focus in a view controller

There are 2 forms in my program. When view controller A gets the focus and be active, view controller B will lost the focus and be inactive. Can anyone show me What the events are?
Thanks
Miken, it depends on what type of objects you are using for your "forms".
The simplest "form" to use is a UITextField, and in that case, events will be sent to your UITextField's delegate. In a lot of simple cases, you will designate the viewController that holds your UITextfield to be the UITextFieldDelegate. For more information on the methods that the delegate has, take a look at this: https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intf/UITextFieldDelegate
In this case, when the text field gets the focus (ie the user clicks on the text field to edit it) the delegate methods:
– textFieldShouldBeginEditing:
– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
will be called. I'm using this as a basic example, and not assuming too much here, but generally you should be looking into your "forms" delegate methods.

MBProgressHUD tap to cancel, longer text?

I'd like to use MBProgressHUD (or similar look) as alternative to default UIAlertView.
I need a canceling capability on this view.
I tried adding the following method to MBProgressHUD class but it didn't get called when touched.
Any idea?
(void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*)event
I can't use gesture recognizer since my lowest target version is 3.1.2.
Also, it seems complex to enlarge label size for MBProgressHUD's text.
Are there altanatives than fixing MBProgessHUD for the purpose?
I just had quick look at MBProgressHUD and would use that. First, change the size of the HUD by modifying layoutSubviews in MBProgressHUD.h. I would then create a new button class (UIButton subclass) and add this as a subview of the HUD.
This is a super old thread, but it would be way easier just to set hud's UserInteractionEnabled:YES and add a tapGestureRecognizer to it.
Cheers.

How can I make an unknown UITextField resign first responder?

My view has two UITextFields and a UISwitch. If a user is edits a textField, and then immediately touches the switch (without pressing return), the text is left as they typed it, without AutoCorrect.
If I know which textField they were typing in, I can force the autocorrect to complete by calling [textField resignFirstResponder]. But the user could be typing in either textField, so I don't know which one to call.
How can I get around this? Is there a way of detecting which textField was being used? Or something simpler I haven't thought of?
One lovely way of doing this without having to keep track of which field is active:
// This causes the current responder (eg. an input field) to resignFirstResponder and
[self.endEditing:YES];
Replace [self.view endEditing:YES] with the below one...
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
The uitextfielddelegate methods are called for the textfield on which the editing is in progress. So that way you needn't be facing the problem of detecting which text field is being edited.
So implement the uitextfielddelegate methods and assign the delegate of the text field to the class where you implement the methods and handle the responses in them.
The methods which you should be interested in are:
textFieldDidEndEditing:
Tells the delegate that editing stopped for the specified text field.
- (void)textFieldDidEndEditing:(UITextField *)textField
Parameters
textField
The text field for which editing ended.
Discussion
This method is called after the text field resigns its first responder status. You can use this method to update your delegate’s state information. For example, you might use this method to hide overlay views that should be visible only while editing.
Implementation of this method by the delegate is optional.
Availability
Available in iOS 2.0 and later.
Declared In
UITextField.h
You may keep track yourself which one is the current one, by using the textFieldDidBeginEditing delegate.

Is there a NSNotification for objects that become first resoponder?

Is there a NSNotification for objects that become first responder.
Like NSNotification that give me the UITextfield that cause the keyboard to pop up?
Check UITextFieldTextDidBeginEditingNotification, the textField that started editing is in notification's object property.
There're also UIKeyboardWillShowNotification and UIKeyboardDidShowNotification notifications
Another option is to have your view controller be a delegate to the UITextField. UITextFieldDelegate has a textFieldDidBeginEditing: method.
No, But you could check it manually by using "IsFirstResponder" (BOOL)..
Can you explain a little bit more what you want to accomplish? Maybe there is a way to use another notification.

UIViewController not receiving touchesBegan message

I have a pretty simple UIViewController. It's initialized with a view I've created in Interaface Builder, which contains only a UIImageView. When the user touches the screen, I want the touchesBegan message of UIViewController to get called. So, I override it and added some logging, but nothing has happened.
I haven't done anything "special" at all, as since UIViewController inherits from UIResponder, I expect this to work right out of the box. From what I understand UIImageViews have user interaction disabled by default, so I have enabled it, both via InterfaceBuilder and in my UIViewcontroller's viewDidLoad method (I have tied the UIImageView to an IBOutlet). I also am ensuring that userInteraction is enabled in the parent view in Interface Builder.
Anything else that I am forgetting here?
OK, I'm a dummy. It works fine. The problem was, I didn't realize I was sending a release message to the UIViewController without having retained it elsewhere first. So that was causing the problem.
It is hard to say what your problem is, I don't know what you mean by overriding it?
Make sure you are connecting the touchesBegan event with an IBAction in Interface Builder?
You must create a IBAction function to handle the event, and explicitly connect the even to the IBAction. It is not as simple as simply overriding a method.
Although you have the View tied to an IBOutlet, you need to connect the event using an IBAction, or else you won't get any of those events.
Please ignore the first answer. It is, in fact, as easy as overriding the method in the custom view. The most common reason for not receiving touchesBegan is the canBecomeFirstResponder method not being implemented. This is not an IB hookup, these are the standard methods for touch handling.