PDA

View Full Version : [iPhone] How to set focus to a specific GUI element?




bejo
Apr 20, 2008, 02:06 PM
I'd like to set focus to a UITextField control (enforce editing mode) just after loading a window (view). Is it possible? I've done some google and apple's doc searching but with no luck :(



iphoneGuy
Apr 20, 2008, 04:13 PM
I'd like to set focus to a UITextField control (enforce editing mode) just after loading a window (view). Is it possible? I've done some google and apple's doc searching but with no luck :(

[myTextField becomeFirstResponder];

bejo
Apr 20, 2008, 05:43 PM
Thank you for a quick answer. My text field gets focus, however the screen keyboard doesn't apear :(. I also tried overriding the following method in my view controller, with no success:
- (void)viewWillAppear:(BOOL)animated {
[fldKeyword becomeFirstResponder];
}(fldKeyword outlet is of course properly connected to the UI element)

iphoneGuy
Apr 20, 2008, 05:48 PM
Thank you for a quick answer. My text field gets focus, however the screen keyboard doesn't apear :(. I also tried overriding the following method in my view controller, with no success:
- (void)viewWillAppear:(BOOL)animated {
[fldKeyword becomeFirstResponder];
}(fldKeyword outlet is of course properly connected to the UI element)

This is what I do in my loadView(). Works perfect...

[fldKeyword becomeFirstResponder];
[self.view addSubview:fldKeyword];

bejo
Apr 20, 2008, 06:13 PM
This is what I do in my loadView(). Works perfect...

[fldKeyword becomeFirstResponder];
[self.view addSubview:fldKeyword];I assume that loadView() is an overridden method of your View that you created programatically. Is it possible to do the same trick with the Interface Builder, where the UIView class and its instance is created by IB, not programatically? (and only the view controller is available for me from Xcode)

TPALTony
Apr 3, 2009, 07:23 PM
I assume that loadView() is an overridden method of your View that you created programatically. Is it possible to do the same trick with the Interface Builder, where the UIView class and its instance is created by IB, not programatically? (and only the view controller is available for me from Xcode)

I don't believe so, but there's a simple enough workaround...

Declare a property in your View controller as an IBOutlet

IBOutlet UITextField *myFirstFocusField;

In Interface builder, drag an association from the text field you want to have the first focus, to Files Owner and associate with "myFirstFocusField"

That will give you the association you want, so you can use the line of code [myFirstFocusField becomeFirstResponder]; in your viewDidLoad method.

You don't need to do the addsubview line as that's all taken care of.