简体   繁体   中英

Programmatically adding UITextField to a dropdownlist

I have a tableview controlled by a UITableViewController. In this UITableViewController class I have added a UITextField programmatically to my UINavigationItem by adding these lines to my viewDidLoad method :

CGRect passwordTextFieldFrame = CGRectMake(20.0f, 100.0f, 280.0f, 31.0f);
UITextField *passwordTextField = [[UITextField alloc] initWithFrame:passwordTextFieldFrame];
passwordTextField.placeholder = @"Password";
passwordTextField.backgroundColor = [UIColor whiteColor];
passwordTextField.textColor = [UIColor blackColor];
passwordTextField.font = [UIFont systemFontOfSize:14.0f];
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
passwordTextField.returnKeyType = UIReturnKeyDone;
passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
passwordTextField.tag = 2;
passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordTextField.delegate = self; // ADD THIS LINE
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:passwordTextField];

The UITextField is displayed correctly on my navigationbar but when I click it the textFieldDidBeginEditing never fires.

I have added the to my header file along with the textFieldDidBeginEditing method.

I tried doing the exact same thing in a view controlled by UIViewController and here the textFieldDidBeginEditing gets fired when I click the "textfield" - So I suspect the fact that I am adding the textfield to a UITableViewController is what's causing trouble.

You might think - why didn't you add a "uinavigationbutton" to the "NavigationBar" instead - but I can't as the uidropdownmenu that I want to call when the button is being clicked only accepts a "UITextField".

尝试再添加一行:

[passwordTextField becomeFirstResponder];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM