2012-06-12 109 views
1

首先我對xcode很陌生。我正在嘗試將多個警報添加到單個視圖。我正在爲ipad創建一個表單,允許用戶在文本框中輸入信息。由於需要填寫多個文本框,因此我不希望多個警報框彈出顯示每個錯誤,但是我想要一個警報視圖顯示多個錯誤。樓下的註釋代碼是我想象它會被寫入在Xcode中添加多個警報到單個警報視圖

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

}

*是否有可能把前是否和消息之後的語句才能夠做到這一點?

回答

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]; 
} 
+0

您可以根據您的信息的要求追加字符串信息顯示給用戶。 –

+0

不太清楚你的意思,如果你編輯我的答案,我會很高興地接受它! –

+0

幫忙,謝謝! – Nick

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