我想在其上显示带有搜索栏选项的警报视图。如何在警报视图中创建搜索栏?任何示例代码都会有所帮助。
答案 0 :(得分: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 :(得分: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;
}