ios - textFieldShouldReturn 未被调用(以编程方式加载 textField)

标签 ios objective-c uitextfield

我目前正在以编程方式加载 -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/

相关文章:

ios - Swift 获取 UITextField 占位符标签颜色

ios - 在越狱的应用程序中以编程方式编辑 Info.plist

android - 在 iOS 的 Browserstack 中查找元素

ios - 如何访问 C 函数中的对象

objective-c - CGContext 无效上下文 0x0

cocoa-touch - 文本字段文本绘制在底部而不是中心

ios - iOS10 中的 UITextField textColor 更新,而不是 iOS 11

ios - 来自 UIWebView 的本地内容 URL

iphone - 有没有办法更改 Xcode 中的括号位置和缩进?

iOS 11 UISearchBar : left + rightbarbuttonitem vertical center issue