how to know whether a UITextView has a focus or not - iphone

I know the becomeFirstResponder method to set a focus on a control, but how to know whether that control (for ex. UITextView) is currently having focus on it or not ?
EDIT: the problem is that I want (for example) to change the background color of the UITextView when it receives focus, where should I put the isFirstResponder call exactly ? should I use notifications in this case ?
thanks so much in advance.

if([txtView isFirstResponder]){
//Has Focus
} else {
//Lost Focus
}
This is UITextView's delegate method,
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView{
//Has Focus
return YES;
}
This is lost focus
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:#"\n"]){
//Lost Focus
[textView resignFirstResponder];
}
return YES;
}

Use the UITextField delegate methods. When textField got focus the(bacame first responder)
- (void)textFieldDidBeginEditing:(UITextField *)textField; will be fired,
and when it lost focus
- (void)textFieldDidEndEditing:(UITextField *)textField; will be fired.

Related

How to hide Textbox Keyboard when "Done / Return" Button is pressed Xcode 4.2

I created a Text Field in Interface Builder. I set it's "Return Key" to Done. This is a one line only input (so it wont need multiple lines).
How do I hide the virtual keyboard when the user taps the done button?
Implement the delegate method UITextFieldDelegate, then:
- (void)viewDidLoad {
[super viewDidLoad];
self.yourIBtextField.delegate = self;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
UITextView does not have any methods which will be called when the user hits the return key.
Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.
There might be other ways but I am not aware of any.
Make sure you declare support for the UITextViewDelegate protocol.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
Make category on UIViewController with next method
- (void)hideKeyboard
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
}

Dismiss keyboard for UITextView without using Return key

I'm using a UITextView and want to keep the normal usage of Return key, i.e. to insert a new line. But how do I dismiss the keyboard when I can't use the Return key for that?
A lot of people add a UIToolbar with a Done button on it above the keyboard. This is how the Safari app does it as well and in my opinion it is the best way to handle this situation. See a pic here.
To dismiss the keyboard, you just have to do [textField resignFirstResponder];.
Here is an okay example of how to add the UIToolbar when the keyboard shows/hides.
How the user triggers it is a design decision: another button, a swipe gesture?
When it's triggered, call:
[self.textView resignFirstResponder];
You could just use a background tap to hide the keyboard.
A good way to Dismiss keyboard.
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
//NSLog(#"ShouldBeginEditing");
if(textView == indexOneTW)
{
here use uibutton and set its target to a method which contains
[urTextView resignFirstResponder]
}
return TRUE;
}
Make sure you declare support for the UITextViewDelegate protocol.
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text
{
if([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
Enjoy Buddy with this code....

resign keyboard in UITextField

I'm working with storyboards on iOS 5 and have a simple screen that has a UITextField on it. I want to dismiss the keyboard when the user hits "Return". I followed other suggestions such as having my controller implements the UITextFieldDelegate protocol and implements textFieldShouldReturn: as follows:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[questionField resignFirstResponder];
return YES;
}
However I see that this method is never called. I have set the controller of my view in storyboarding to my custom UIViewController.
I also tried a different implementation where I create an IBAction called dismissKeyboard but oddly I can't connect the Did Exit action to my UITextField.
Any ideas?
Update : So the problem seems to be that I'm using a UITextView and not a UITextField. I wanted a large area for the text to be entered. When I change the entry field to a UITextField it works fine. Any ideas on how to make it work with a UITextView?
You should connect the dismiss method to the text field's Did End Editing action.
So the solution to resign the keyboard for a UITextView is to implement shouldChangeTextInRange.
If you want the keyboard to dismiss when tap the return key for the textview use this delegate function,
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}

How to hide keyboard once UITextView becomes the first responder in iPhone SDK?

In my iPhone App when I click on UITextView keyboard becomes visible
I try to use resignFirstResponder on "textDidEndOnExit" event but the keyboard does not hide.
What should be done to hide the keyboard?
please Help and Suggest,
Thanks.
Here another thread about this topic:
how to hide the keyboard when empty area is touched on iphone
i think this can help you.
I would suggest you to keep a toolbar and inside a button called "Dismiss" just above the keyboard. resign your responder and hide the toolbar when dismiss button is clicked. In the textView textViewShouldBeginEditing show the toolbar. Default hide the toolbar.
If you want your UITextView to not allow carriage returns, and close when the user presses the return key (or Done if you have changed the return key type) then implement the UITextViewDelegate protocol and use something like:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
oneway, you can also hide keyboard when touch in view screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[txtDetail resignFirstResponder];
}
}
Make a UIButton or UiBarButton and assign it a method in which write [textView resignFirstResponder];
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return NO;
}
return No; wont allow your keyboard to appear.Hope it helps.You will not require any toolbar or something.Give it a try...

iPhone: Possible to dismiss keyboard with UITextField clear button?

I'm wondering if there is a way to have the UITextField clear button 'always visible'
textfield.clearButtonMode = UITextFieldViewModeAlways;
doesn't seem to work
.. and if it is possible to dismiss the keyboard using the button?
Thanks in advance.
Like mentioned before it seems apple is setting the textfield focus after you clear the field.
The solution is quite simple. Just clear the field yourself, resignFirstResponder and return NO
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
In your delegate, the function
- (BOOL)textFieldShouldClear:(UITextField *)textField
is called when the users wants to clear the textfield. If you return YES and call
[textField resignFirstResponder];
the keyboard should go away. I don't know about the clearButtonMode, other than that you may want to set it early, preferably before adding the view to its superview.
edit To make sure you really resign the responder, try doing it just a little later:
[textField performSelector:#selector(resignFirstResponder) withObject:nil afterDelay:0.1];
The delay didn't work well for me. Instead I added an instance variable to the delegate:
BOOL cancelEdit;
Then in the delegate implementation:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (cancelEdit) {
cancelEdit = NO;
return NO;
} else {
return YES;
}
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
cancelEdit = YES;
return YES;
}
UITextFieldDelegate textFieldShouldClear
- (BOOL)textFieldShouldClear:(UITextField *)textField {
[textField] resignFirstResponder];
return YES;
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldClear:
I discovered this odd behavior was caused by a competing gesture recognizer that resigned the first responder before the keyboard before textFieldShouldClear: was called. It seemed to be corrupting the first responder.
If you've set it up this way, ensure that cancelsTouchesInView on your gesture recognizer is set to YES. This way you shouldn't need to do anything special in the textFieldShouldClear: or textFieldShouldBeginEditing: delegate methods.