Add three Buttons in an UIAlertView
In iOS11 / Swift 4, it is as simple as this:
let alert = UIAlertController(title: "Saving Demo.", message: "Do you wish to save this Demo Settings?", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: {
(_)in
//SAVE DEMO DATA HERE
})
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: {
(_)in
//do something
})
let thirdAction = UIAlertAction(title: "3rd Btn", style: UIAlertActionStyle.default, handler: {
(_)in
//do another thing
})
alert.addAction(OKAction)
alert.addAction(cancelAction)
alert.addAction(thirdAction)
self.present(alert, animated: true, completion: nil)
You can also make your class the delegate of UIAlertViewDelegate and reach all of your buttons" functionalities by doing so;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
//
}
else if (buttonIndex == 1) {
//
}
else if (buttonIndex == 2) {
//
}
else if (buttonIndex == 3) {
//
}
}
If you are really struggling to find the solution, the following code may help you.
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Refresh"
message:@"Are you want to Refresh Data"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", @"Done", nil];
[alert show];
[alert release];