问题
I wanna know how can I make 2 UIAlertView, with 3 buttons, the UIAlertViews(2) needs be different, the options and the actions... how ???
回答1:
Try this:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Message." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Button 2", @"Button 3", nil];
alert.tag = 1;
[alert show];
then do the same for the next alertView, just change the tag to 2
Then just run this method
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alert.tag == 1) {
//the alert tag 1 was just closed - do something
}
}
Also - make sure you include the UIAlertViewDelegate
回答2:
Just check 2 in the alertviews(4) delegate method which alertviews was responsible for the method to be called(1).
回答3:
UIAlertview delegates are deprecated in ios 9.0
Also when you add more then 2 buttons, they will be vertically assigned by IOS.
you can do with simply UIAlertController
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:[[[NSBundle mainBundle] infoDictionary]
objectForKey:@"CFBundleDisplayName"]
message:@"share via"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* fbButton = [UIAlertAction
actionWithTitle:@"Facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// Add your code
}];
UIAlertAction* twitterButton = [UIAlertAction
actionWithTitle:@"Twitter"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// Add your code
}];
UIAlertAction* watsappButton = [UIAlertAction
actionWithTitle:@"Whatsapp"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// Add your code
}];
UIAlertAction* emailButton = [UIAlertAction
actionWithTitle:@"Email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
// Add your code
}];
UIAlertAction* cancelButton = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Handel no, thanks button
}];
[alert addAction:fbButton];
[alert addAction:twitterButton];
[alert addAction:watsappButton];
[alert addAction:emailButton];
[alert addAction:cancelButton];
[self presentViewController:alert animated:YES completion:nil];
来源:https://stackoverflow.com/questions/5022344/2-uialertview-with-3-buttons