2

使用搜索选项卡时,我的搜索提醒出现问题。当有人输入他们的搜索字符串并按回车键时,它只会关闭键盘,您仍然必须点击“搜索”(Zoek)按钮才能真正开始网络搜索。

这是我的代码:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *inputText = [[alertView textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
    [searchview addSubview:indicator];
    NSString *urlAddress= @"http://www.sportdirect.com/articles?search=";
    urlAddress =[urlAddress stringByAppendingString:inputText];
    NSURL *url= [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
    [searchview loadRequest:requestObj];
    [[searchview scrollView] setBounces: NO];
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
    [TestFlight passCheckpoint:@"Voerde zoekopdracht uit"];

}
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
[_message dismissWithClickedButtonIndex:(0) animated:YES];
NSString *inputText = [[_message textFieldAtIndex:0] text];
if( [inputText length] >= 1 )
{
    [searchview addSubview:indicator];
    NSString *urlAddress= @"http://www.sportdirect.com/articles?search=";
    urlAddress =[urlAddress stringByAppendingString:inputText];
    NSURL *url= [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj= [NSURLRequest requestWithURL:url];
    [searchview loadRequest:requestObj];
    [[searchview scrollView] setBounces: NO];
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
    [TestFlight passCheckpoint:@"Voerde zoekopdracht uit"];

}

[textField resignFirstResponder];
return NO; // We do not want UITextField to insert line-breaks.
}

有谁知道一旦键盘上的返回按钮被选中,如何实际进行搜索?

提前致谢!

4

2 回答 2

0

您的类需要实现UITextFieldDelegate以获取键盘返回回调。正如borrden所建议的,这可以按如下方式完成

[message textFieldAtIndex:0].delegate = self;

然后实现这个方法,

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Dismiss the alert view 
    // Initiate the search
    return YES;
}

使用此UIAlertView方法。参考苹果文档

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

当需要以编程方式关闭它时,在您的警报视图实例上调用它。您需要保留message为 ivar 才能在需要时访问它。

希望有帮助!

于 2013-09-18T08:31:19.033 回答
0

您可以在UITextField委托方法中执行此操作:

你的班级必须实现UITextFieldDelegate. 放textField.delegate = self;

实现此方法后:

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
  // here you can write what you want, for example search

  [textField resignFirstResponder];

  return NO; // We do not want UITextField to insert line-breaks.
}
于 2013-09-18T08:36:03.437 回答