2010-06-15 95 views

回答

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]; 

如果还有其他更好的方法,请让我知道。

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; 
} 
相关问题