UITextField focus - iphone

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()

Related

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.

iPhone- After resigning first responder on UITextField, can't refocus it

I have a modal window that's used for searching data from a remote server- it has a UITextField as the titleControl of the navbar for the window, and a tableview filling the window (that displays the results obviously). Now what I want to do is when the user scrolls the tableview, immediately have the textfield lose focus (resign first responder) so that the keyboard dismisses and the user has more room to scroll through the tableview (it stretches down to fill the gap left by the keyboard). Basically the same functionality as when using a UISearchDisplayController (or whatever it's called).
So I have this code for detecting the scroll event of the tableview:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[searchField resignFirstResponder];
}
Which works fine. However, the issue is that once the user scrolls the table and the textfield loses focus, you can't give focus back to it by tapping on it again. So basically once I call that [resignFirstResponser] I can never again bring the keyboard back up and edit the textfield value. Anyone have any idea why? Do I need to explicitly call [becomeFirstResponder] on the field somewhere? Because I thought that was handled automatically when the field is tapped?
Also of note- I am calling [becomeFirstResponder] on the text field right when the modal window is first called up, so the field is pre-focused. Could that have anything to do with it?
I can post more code if anyone would like, but I don't think I'm doing anything out of the ordinary with the textfield.
Thanks for any help!
You are calling the resignFirstResponder from a function which will be called everytime you scroll the UIScrollview. Hence it does not appear. You need to call resign when the uitextview goes out of focus.
You can do the following. Its a hack:
Whenever you focus on the UITextField create a invisible button to overlay your scroll view.
Capture the button press event and resign first responder
Whenever the uitextfield becomes first responder create the button
This way you will remove the bug, viz calling the method in scrollViewWillBeginDragging.
Other option would be to overrite viewDidAppear method for the uiTextField.
Or you could put your textfield into a different container and handle scrollViewWillBeginDragging by checking which scrollview sent the message.
Did u set a delegate for you searchField? I had the same issue. I popup a model view, and set the text field to be the first responder inside viewDidLoad. Everything works well for the first time. But once I dismiss the modal view controller, and reopen it. my text field cannot be focused anymore.
I found it has something to do with methods of UITextFieldDelegate. Once I remove implementation for methods
– textFieldShouldEndEditing:
– textFieldDidEndEditing:
everything works well. but don't know why
Are you doing anything with "textFieldShouldEndEditing", like #fengd?
A problem that I had was that I was viewing a modal view, and my "textFieldShouldEndEditing" routine was incorrectly returning "NO" on a specific text field. When my modal got dismissed, I would be unable to tap on any other text-field, presumably because the old text field was still "first responder". Since it can never end editing, it fouls up all other text fields that come after it.
I realize this is 2 yrs after the fact, but maybe someone else might find this useful.

how to open new view on touch even for uitextfield

I am just woundering if it is possible and if so how I could go about loading a new
view when the user touches a uitextfield?
What I am trying to do is load a new view where the user will use a picker to populate the UItextField instead of typing the word.
any help would be greatly appreciated
What you need here is the inputView property of UITextField. If you set this for a text field, then instead of the usual keyboard, the inputView object will pop up. I had created this example for a related problem which uses a UIPickerView as an inputView to a UITextField object. You can check it to see if it helps.

UIPicker in inappsettingskit

Using inappsettingskit, I'm searching for a way to display an uipickerview (with custom values) when clicking on a textfield cell (instead of a keyboard).
Is there any way to do so ?
Thanks for your help :)
Read the setting in you application and for showing picker view instead of standard keyboard use UITextField inputView property.
myTextField.inputView = myPickerView;
From Apple Documentation for inputView .
If the value in this property is nil,
the text field displays the standard
system keyboard when it becomes first
responder. Assigning a custom view to
this property causes that view to be
presented instead.

How can I show a UIDatePicker instead of a keyboard when a user selects a UITextField?

I have a nice clean UI within a table view which has a few text fields for the user to fill out. One of the fields is for the user's birthday.
I'd like to have it so that when the user selects the birthday field, a view containing a UIDatePicker would come up as, just like the different keyboards do when selecting a text field.
Can this be done? I would have to prevent the text field from being the first responder (to avoid having the keyboard come up) and I would have to animate the view sliding up if no keyboard was showing before.
Would presenting the view modally be an option? If so how would I go about doing it? From the documentation it seems that modal views still take up the whole screen, I just want to use the lower 216 pixels (height of the keyboard and UIDatePicker).
Any one have any tips on how to go about doing this?
Old question but the correct way to do this these days would be to set the UITextField's inputView to a picker you created somewhere. Something like this:
UIPickerView *myPicker = [[UIPickerView alloc] init];
// set picker frame, options, etc...
// N.B. origin for the picker's frame should be 0,0
[myTextField setInputView:myPicker];
When you go to edit a UITextField, iOS really just displays whatever view is at textField.inputView which by default is the keyboard, you can make it anything you want as long as it's a subclass of UIView.
Regarding animation, take a look at DateCell sample application -
http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html
And in any case, the proper way to do this is set UITextField's inputView to show the picker instead of the keyboard. That's what it's meant to do. More on that here:
How can I present a picker view just like the keyboard does?
Cheers,
Oded.
I would implement this by just animating a view containing the UIDatePicker, a Done, and Cancel button) up from the bottom of the screen. Using CoreAnimation, this should be pretty easy.
Why are you using a text field if you don't want to accept user input from a keyboard? Instead use a UILabel subclass (where you override the touchesBegan/Ended:withEvent: set of methods to show the UIDatePicker) or a UIButton (where your action is a method which slides up the UIDatePicker).