我目前正在以编程方式加载 -UITextField
并尝试在单击“搜索”后让键盘按下。
我将 .m 文件中的 -UITextFeild
声明为
UITextField *searchTextField;
在 .h 文件中,我确实声明了 -UITextField
的委托(delegate)
@interface firstChannel : UIViewController<LBYouTubePlayerControllerDelegate, UITableViewDelegate,UITableViewDataSource, NSXMLParserDelegate, UITextFieldDelegate, AVPlayerItemOutputPullDelegate>{
这就是我以编程方式添加 UITextField 的方法。
searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, -26, 300, 30)];
searchTextField.borderStyle = UITextBorderStyleRoundedRect;
searchTextField.font = [UIFont fontWithName:@"Heiti TC" size:13];
searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
searchTextField.placeholder = @"Search on the Channel";
searchTextField.borderStyle = UITextBorderStyleNone;;
searchTextField.textAlignment = UITextAlignmentCenter;
searchTextField.center = CGPointMake(160, 43);
searchTextField.textColor = [UIColor colorWithRed:(232.0/255.0) green:(232.0/255.0) blue:(232.0/255.0) alpha:(100.0/100.0)];
searchTextField.clearButtonMode = UITextFieldViewModeNever;
searchTextField.returnKeyType = UIReturnKeySearch;
[searchTextField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.view.window addSubview:searchTextField];
[searchTextField becomeFirstResponder];
我在 -viewDidLoad
中添加了委托(delegate)
searchTextField.delegate = self;
但是,当我单击键盘上的“搜索”时,没有任何反应。我不确定为什么会发生这种情况,我什至通过在按下回车键后记录一些内容来调试它,但没有任何反应。
最佳答案
创建并设置文本字段后调用searchTExtField.delegate
。
关于ios - textFieldShouldReturn 未被调用(以编程方式加载 textField),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602104/