Adding Multiple Alerts to a Single Alert View in Xcode
Question
First off I am fairly new to xcode. I am trying to add multiple alerts to a single view. I am creating a form for an ipad which allows the users to enter in information into text boxes. Since there are multiple text boxes that are required to fill out, I don't want multiple alert boxes to pop up displaying every error, but instead I want one alert view to show multiple errors. the commented code down below is how I imagine it might be written
- (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];
}
*Is it possible to put if statements before and after the messages to be able to do this?
La solution
Do it like this:
- (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];
}
Autres conseils
You can append message multiply by doing this:
- (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];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow