NOTA:
Importante: UIAlertView está en desuso en iOS 8. (. Tenga en cuenta que UIAlertViewDelegate también está en desuso) para crear y administrar alertas en iOS 8 y versiones posteriores, en lugar de utilizar UIAlertController con un preferredStyle de UIAlertControllerStyleAlert.
Please check this out tutorial
"deprecated" means???
Objectvie C
archivo .h
@interface urViewController : UIViewController <UIAlertViewDelegate> {
archivo .m
// Create Alert and set the delegate to listen events
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete"
message:@"Press OK to submit your data!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
// Set the tag to alert unique among the other alerts.
// So that you can find out later, which alert we are handling
alert.tag = 100;
[alert show];
//[alert release];
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// Is this my Alert View?
if (alertView.tag == 100) {
//Yes
// You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons.
// Then u can check which button was pressed.
if (buttonIndex == 0) {// 1st Other Button
[self submitData];
}
else if (buttonIndex == 1) {// 2nd Other Button
}
}
else {
//No
// Other Alert View
}
}
Swift
La forma Swifty es utilizar el nuevo UIAlertController y cierres:
// Create the alert controller
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
NSLog("OK Pressed")
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)