代码之家  ›  专栏  ›  技术社区  ›  Warrior

如何在iPhone中使用搜索栏创建AlertView?

  •  0
  • Warrior  · 技术社区  · 14 年前

    我想显示一个带有搜索栏选项的警报视图。如何在警报视图中创建搜索栏?任何样本代码都是有用的。

    2 回复  |  直到 6 年前
        1
  •  3
  •   WrightsCS    13 年前

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

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

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

        2
  •  1
  •   bentford Marko Hlebar    12 年前

    有更好的方法:

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