2

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

-UITextFeild将 .m 文件的内部声明为 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;

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

4

3 回答 3

4

searchTExtField.delegate在创建和设置文本字段后调用。

于 2013-09-04T11:31:26.990 回答
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,那么您应该没问题。

于 2013-09-03T21:57:19.230 回答
0

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

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

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

于 2013-09-05T10:18:05.503 回答