Setting a UITextField into editing mode programmatically - iphone

I have a UITextField that I want to set into editing mode (keyboard on screen and cursor in text field box) programatically. I know that the user will be in editing mode when this view appears onscreen, so I want to save the user from having to tap the text field.
The "editing" property of a UITextField is read only - so that doesn't work. Is there a way to set the UITextField into editing mode, with a keyboard onscreen, programmatically?

Call becomeFirstResponder on the UITextField.
Related question:
How do I show the keyboard by default in UITextView?

You have to call [textField becomeFirstResponder];

Indeed call [textField becomeFirstResponder] in Obj-C or textField.becomeFirstResponder() in Swift.
However, make sure you call this in the viewDidAppear and not in the viewDidLoad to prevent strange behaviour (see: When set UITextField as FirstResponder programmatically, cause some weird actions on text editing).

Related

Show UITextField keyboard on firstResponder even when userInteractionEnabled = NO

I have a UITextField that is first responder. I want to show keyboard when entering the view but I want to do that the user will not be able to edit it and the cursor will be hidden all time as well.
When you click on a keyboard letter, it will be written in the UITextField, but the user will not be able to edit nothing there, even not to copy.
Thanks!
Ok, per my comment, my solution is to have a surrogate UITextField that has its hidden property set to YES. What I do is add that hidden text field to the view, and call becomeFirstResponder on it. The user has no idea this text field exists. In the delegate callback from the text field, I take the text the user typed in and add it to a UITextView (though you could add the text to whatever you wanted, like a UITextField like in your question). I turn off userInteractionEnabled for the visible text view. This creates the effect you desire.
I created a sample project that I uploaded to Github. (If you aren't familiar with it, just click the zip button to download it, unzip it, and open the .xcodeproj file). https://github.com/MaxGabriel/HiddenTextField
I had a UISearchBar property in my viewController. And I did it like this:
- (void)viewWillAppear:(BOOL)animated
{
[self.searchBar becomeFirstResponder];
}
This should work the same for a UITextField.
As for disabling editing, use:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
You should have set your viewController to be the delegate of UITextField.
Edited answer: Try this:
1. [txtField becomeFirstResponder];
2. txtField.enabled = NO;
3. when some press on keyboard, then txtField.enabled = YES;
Check this out : http://www.youtube.com/watch?v=cKV5csbueHA

UITextField Hide Keyboard But Reamin First Responder?

I've subclassed a UITextField to display a UIDatePicker instead of a keyboard. Entering dates is something that happens often I our app. The problem occurs when another of our custom classes that accommodates the keyboard needs to know what the first responder.
Is there a way to remain first responder, whilst hiding keyboard?
No, you have to resign as responder (give up focus) to dismiss the keyboard.
Edit: It seems I lied. Try [self.view endEditing:YES];
FYI: It only works on iOS 3.2+
Actually, instead of subclassing, you should just make a custom inputView for your text field which uses a date picker as the custom "keyboard". Then, it will remain the first responder and never even call the system keyboard in the first place.

after inputAccessoryView tapped

I have tried the solutions at UITextView doesn't show InputAccessoryView on first click
I'm having a with a problem with a textView who has a inputAccessoryView
I want to call [textView resignFirstResponder] through its inputAccessoryView,so that i can close the keyboard
and there is only a UIButton on the inputAccessoryView,and the tapping the button will invoke an IBAction method call -(IBAction)closeKeyboard:(id)sender;
now the sender in the method is the button on the inputAccessoryView,
question is ,
how can i find out this textView whose inputAccessoryView has been tapped,
or just get a pointed which is pointed to this textView,so i can call
[textView resignFirstResponder]??
You need to find the current first responder. If you have outlets to all your text views, you can ask each one in turn
if ([textView1 isFirstResponder])
[textView resignFirstResponder];
Though this may give you problems depending on your button (see here, but I haven't experienced this), if so use the editing property of the text view.

How to retract the Keyboard on touching the UITextView when keyboard is already up?

I am writing an application that has a UITextView which allows editing. When a user first touches UITextView, a keyboard shows up and I want to retract that keyboard when user again touches the UITextView e.g. I have entered some data in a textview and with keyboard still showing on the screen I tap on the UITextView which should cause the keyboard to retract.
Is there any way to achive this?
(I'm aware of providing a done button and doing this but I want to achive this by tapping on UITextView itself)
As an aside, I would urge you not to use toggle state elements on the iPhone. It's to easy to double tap in real world use. That is why the Apple apps all use the either the "return" key on the keyboard or the done button.
In the interface you contemplate, the users will find themselves closing and then accidentally reopening the keyboard about 10% of the time or more. It will make your app feel cumbersome and flaky.
You should call resignFirstResponder for the UITextView. Let's say you have an IBOutlet for the text view:
#property (nonatomic, retain) IBOutlet UITextView *comment;
Then [comment resignFirstResponder]; can be called from a touchesBegan or the like.
See e.g. How to Dismiss the Keyboard when using a UITextView.
What you are looking for is a large transparent uibutton "overlapButton" which always stays on top of the uitextview.
When the textview appears, you set the button hidden so you can tap on the textview freely.
[overlapButton setHidden:YES];
When tapping the textview, the keyboard will come up and the following method inside your textview delegate will get called:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[overlapButton setHidden:NO];
}
Here, you need to set visible the "overlapButton" so that while the keyboard is up, you can touch the button which now overlaps the textview. On the button action, you can hide the keyboard:
-(IBAction) overlapButtonTapped{
[myTextView resignFirstResponder];
}
After resigning the first responder, the following method will get called:
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[overlapButton setHidden:YES];
}
After setting the hidden property accordingly for the button (like above), you have a "clear" textview again which you can tap again to show the keyboard.. etc .. etc ..
Cheers.
I agree with you TechZen. I've seen this happen! I would not advise this also! It also makes editing very hard if not impossible for edit/copy/paste gestures. On the other hand, if the man still wants this badly.. :P the code provided by me in my other post works a treat! Cheers.

UITextField focus

How can I programmatically assign focus to a specific UITextField in a view? I have several fields and on view display, I'd like to put the cursor on a specific field, not the first one at top.
Try doing this in viewWillAppear:
[desiredField becomeFirstResponder];
By making the field the first responder, it have focus and the keyboard will be displayed.
Set the first responder for your view to be the text field. This can be done in IB.
In swift:
desiredField.becomeFirstResponder()