I have a core data application which uses a navigation controller to drill down to a detail view and then if you edit one of the rows of data in the detail view you get taken to an Edit View for the that single line, like in Apples CoreDataBooks example (except CoreDataBooks only uses a UITextField
on its own, not one which is a subview of UITableViewCell
like mine)!
The edit view is a UITableviewController
which creates its table with a single section single row and a UITextfield
in the cell, programatically.
What I want to happen is when you select a row to edit and the edit view is pushed onto the nav stack and the edit view is animated moving across the screen, I want the textfield to be selected as firstResponder so that the keyboard is already showing as the view moves across the screen to take position. Like in the Contacts app or in the CoreDataBooks App.
I currently have the following code in my app which causes the view to load and then you see the keyboard appear (which isn't what I want, I want the keyboard to already be there)
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[theTextField becomeFirstResponder];
}
You can't put this in -viewWillAppear
as the textfield hasn't been created yet so theTextField
is nil. In the CoreDataBooks App where they achieve what i want they load their view from a nib so they use the same code but in -viewWillAppear
as the textfield has already been created!
Is there anyway of getting around this without creating a nib, I want to keep the implementation programatic to enable greater flexibility.
Many Thanks
have you tried using the uinavigationcontroller delegate methods?:
navigationController:willShowViewController:animated:
After speaking with the Apple Dev Support Team, I have an answer!
What you need to do is to create an offscreen
UITextField
in-(void)loadView;
and then set it as first responder then on theviewDidLoad
method you can set theUITextField
in theUITableViewCell
to be first responder. Heres some example code (remember I'm doing this in aUITableViewController
so I am creating the tableview as well!I hope this helps anyone stuck in the same position as me!
If anyone else can see a better way to do this please say.
I think the obvious solution is to create the textfield in the
init
method of the view controller. That is usually where you configure the view because a view controller does require a populated view property.Then you can set the textfield as first responder in
viewWillAppear
and the keyboard should be visible as the view slides in.Try do it in viewDidAppear method, works for me.