[iPhone] How to set focus to a specific GUI element?

Discussion in 'iOS Programming' started by bejo, Apr 20, 2008.

  1. macrumors newbie

    Joined:
    Apr 20, 2008
    #1
    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 :(
     
  2. macrumors member

    Joined:
    Oct 26, 2007
    Location:
    [[NSLocation alloc] initWithCity: Chicago]
    #2
    [myTextField becomeFirstResponder];
     
  3. thread starter macrumors newbie

    Joined:
    Apr 20, 2008
    #3
    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:
    Code:
    - (void)viewWillAppear:(BOOL)animated {
    	[fldKeyword becomeFirstResponder];
    }
    (fldKeyword outlet is of course properly connected to the UI element)
     
  4. macrumors member

    Joined:
    Oct 26, 2007
    Location:
    [[NSLocation alloc] initWithCity: Chicago]
    #4
    This is what I do in my loadView(). Works perfect...
    Code:
    	[fldKeyword becomeFirstResponder];
    	[self.view addSubview:fldKeyword];
    
    
     
  5. thread starter macrumors newbie

    Joined:
    Apr 20, 2008
    #5
    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)
     
  6. macrumors regular

    Joined:
    May 29, 2007
    #6
    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.
     

Share This Page