0

我想显示一个带有搜索栏选项的警报视图。如何在警报视图中创建搜索栏?任何示例代码都会有所帮助。

4

2 回答 2

3

我创建了一个搜索栏并将文本框添加为其子视图。

UIAlertView * myAlertView = [[UIAlertView alloc] initWithTitle:@"         " 
                                                       message:@"         "
                                                      delegate:self 
                                             cancelButtonTitle:nil 
                                             otherButtonTitles:nil, nil];
UISearchBar *myTextField = [[UISearchBar alloc] initWithFrame:CGRectMake(30.0, 35.0, 180.0, 35.0)];
UIButton *searchbtn = [[UIButton alloc] initWithFrame:CGRectMake(230.0, 35.0, 40.0, 35.0)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myAlertView setBackgroundColor:[UIColor grayColor]];
[searchbtn setBackgroundColor:[UIColor grayColor]];
[myAlertView addSubview:myTextField];
[myAlertView addSubview:searchbtn];

如果还有其他更好的方法请告诉我。

于 2010-06-15T23:20:48.947 回答
1

有一个更好的方法:

    UIAlertView *myAlertView = [[[UIAlertView alloc] initWithTitle:@"Keyword Search" 
                                                           message:@""
                                                          delegate:self 
                                                 cancelButtonTitle:nil
                                                 otherButtonTitles:@"Search", nil] autorelease];

    myAlertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    [myAlertView show];

在代表中:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSString *searchTerm = [alertView textFieldAtIndex:0].text;
}
于 2012-04-10T20:29:17.547 回答