2

I have a custom UITextField added to the UINavigationItem.titleView. But it is not becomeFirstResponder. Why so?

- (void) viewDidLoad
{
    [super viewDidLoad];
    UITextField *titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(65.0f, 8.0f, 160.0f, 30.0f)];
    titleTextField.text = @"Untitled";
    titleTextField.textAlignment = UITextAlignmentCenter;
    titleTextField.textColor = [UIColor whiteColor];
    titleTextField.font = [UIFont boldSystemFontOfSize:20.0f];
    //titleTextField.backgroundColor = [UIColor redColor];
    titleTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    titleTextField.delegate = self;
    titleTextField.tag = 2;
    self.navigationItem.titleView = titleTextField;
    [titleTextField becomeFirstResponder]
}

Please advice.

Thanks in Advance, Bharathi.

3 Answers3

1

Try [self.navigationItem.titleView becomeFirstResponder]; instead of [titleTextField becomeFirstResponder];

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
Saad
  • 8,857
  • 2
  • 41
  • 51
0

You also need to conform the UITextFieldDelegate protocol. I think you do not have conform the protocol.

in .h file (for example)

@interface RootViewController : UITableViewController<UITextFieldDelegate>

@end
Maulik
  • 19,348
  • 14
  • 82
  • 137
0

Try this:

[titleTextField becomeFirstResponder];

then:

self.navigationItem.titleView = titleTextField;

tell me if any luck...

Kimpoy
  • 1,974
  • 3
  • 21
  • 38
  • Seems like, navigationItem.titleView will not be added to the view hierarchy.. So it won't respond to UIResponderClass... Do correct me If I am wrong... – Bharathi Jayakumar Jun 13 '12 at 06:11