【问题标题】:Adding Multiple Alerts to a Single Alert View in Xcode在 Xcode 中将多个警报添加到单个警报视图
【发布时间】:2012-06-12 14:35:52
【问题描述】:

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

【问题讨论】:

    标签: xcode alert alertview


    【解决方案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];
     }
    

    【讨论】:

      【解决方案2】:

      这样做:

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

      【讨论】:

      • 您可以根据需要向用户显示的消息附加字符串消息。
      • 不太清楚你的意思,如果你对我的回答进行编辑,我会很乐意接受它!