没有调用textFieldShouldReturn(以编程方式加载textField)

时间:2013-09-03 21:53:11

标签: ios objective-c uitextfield

我目前正在以编程方式加载 - UITextField,并在点击“搜索”后尝试让键盘关闭。

我将.m文件中的-UITextFeild声明为 UITextField *searchTextField;

在.h文件中我确实声明了-UITextField的委托

@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

中添加了委托
searchTextField.delegate = self;

然而,当我点击键盘上的“搜索”时,没有任何反应。我不确定为什么会发生这种情况我甚至通过记录一些东西来调试它,但是没有任何反应。

3 个答案:

答案 0 :(得分:4)

创建并设置文本字段后调用searchTExtField.delegate

答案 1 :(得分:0)

尝试移动viewDidLoad中的所有代码

    - (void)viewDidLoad
    {
        [super viewDidLoad]; 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.delegate = self;
        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];
}

此外,如果您使用搜索栏,则需要使用搜索栏代理。如果您只是使用UITextfield,那么您应该没问题。

答案 2 :(得分:0)

您是否实现了委托方法(当用户点击返回键时会调用此方法):

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   //Perform search with text:
   [self performSearch:[textField text]];
   return NO;
}

这将使响应者辞职。在这里,您也可以调用您的搜索方法。