1

首先,我对 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 语句才能做到这一点?

4

2 回答 2

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];
 }
于 2012-06-12T14:58:40.983 回答
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];
}
于 2012-06-12T14:39:43.023 回答