在Xcode中将单个警报视图添加多个警报

时间:2012-06-12 14:35:52

标签: xcode alert alertview

首先我对xcode很新。我正在尝试向单个视图添加多个警报。我正在为ipad创建一个表单,允许用户在文本框中输入信息。由于需要填写多个文本框,我不希望弹出多个警告框来显示每个错误,而是希望一个警报视图显示多个错误。下面注释的代码是我想象的可能写的

- (IBAction)showMessage:(id)sender {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Name" //"Address" 
                                   //if name=nil  message:@"PLease fill out your name"
                               //if address=nilmessage:@"PLease fill out your address"



delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];

[message show];

}

*是否可以在消息之前和之后放置if语句才能执行此操作?

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式追加消息:

 - (IBAction)showMessage:(id)sender {
    NSString *theMessage = @"PLease fill out your ";
    BOOL nameFlag = FALSE;
    if(name.length == 0)
    {  nameFlag = TRUE // For appending message
      [theMessage stringByAppendingFormat:@"name"];
    }

    if(address.length == 0)
    {
       if(nameFlag){ 
      [theMessage stringByAppendingFormat:@"& address"
       }
       else
       {    
         [theMessage stringByAppendingFormat:@"address"
        }
    }


    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Message"
                                            message:theMessage
                                            delegate:nil
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];  
    [message show];
    [message release];
 }

答案 1 :(得分:0)

这样做:

- (IBAction)showMessage:(id)sender {
        NSString *theMessage = @"";
        if (textField.text.length == 0) {
        theMessage = @"hey";
        } else if (textField2.text.length == 0) {
        theMessage = @"hey2";
        } else if (textField.text.length == 0) && (textField2.text.length == 0) {
        theMessage = @"doubleHey";
        }

        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Name"
                                                message:theMessage
                                                delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];  
        [message show];
        [message release];
}