Is there a NSNotification for objects that become first resoponder? - iphone

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.

Related

removing keyboard from Screen without calling resignFirstResponder

I have a custom View (NotifyView) added on UIWindow with a dismiss button(which remove it from UIWindow).
This view is added when a PUSH notification comes in didReceiveRemoteNotification
there are several cases when I could be on any screen, and my keyboard is UP via UITextfield/UITextview.
At this stage if a push comes, the NotifyView is added on UIWindow behind the keyboard.
I want to resign the keyboard once the PUSH is received so for that I could:
post a Notification with NSNotificaitonCenter to resign all textfields/textviews (if anyone is firstResponder). For this I have to keep active pointer to the currently active textfield/textview in all controllers.
make a variable in AppDelegate and assign the active textfields/textviews to it and on PUSH, and resignFirstResponder of those on PUSH.
Both of the solutions would require making changes to all controller's code and I am looking for something more generic like:
could there be any way through which I could simply remove the Keyboard from the screen on receiving PUSH
or I could fetch the current firstResponder of the application and resign it explicitly.
these could be generic solutions.
It would be really helpful if someone could facilitate this thought process or someone have any immediate solution for this case.
You can use the method below
[[[UIApplication sharedApplication] keyWindow] endEditing:YES]
Try this one , for me it works fine
[yourView endEditing:YES]
The problem it is, when you want to give back the focus.
I have implemented as listening each textfield and caching which is the latest. Than resign only that one ( in push ) after that restore state, require focus for that.
Try this in delegate method...
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self.view endEditing:YES]; // added this in for case when keyboard was already on screen
[self editStartDate:textField];
return NO;
}

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.

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

Programmatically discover identity of first responder

I want a UITextField to be sent the resignFirstResponder message if it is being edited and a user touches elsewhere on the screen. Since there are several text fields I need a way to programmatically determine which one is the first responder to send it the message. How can I do this? Is there some sort of global first responder object?
Thanks,
Jacob
The simplest way is to find the first responder and tell it to resignFirstResponder.
UITextField inherits from UIResponder so you can use isFirstResponder (which returns a BOOL) to query it.
if ([myTextField isFirstResponder]) {
// do stuff
}

How to call a method when the Done Button in the KeyBoard is Clicked?

I want to call a method when the done button is clicked in the UITextField KeyBoard? Please Help me...
It's not even necessary to implement the delegate. I greatly prefer using good, old-fashioned target/action pattern to handle this. It can also lead to cleaner code if you have multiple ways of ending editing (say, intercepting touches outside the text field to cancel editing).
To use target/action, simply wire up UIControlEventEditingDidEndOnExit, which shows up in Interface Builder as the Did End On Exit event.
No muss, no fuss. A lot cleaner and easier than implementing the delegate.
See the UITextFieldDelegate Protocol reference. You probably want to implement the – textFieldShouldReturn: method in your delegate.