/ Published in: Other
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
import UIKit class ViewController: UIViewController, UIAlertViewDelegate { let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue // MARK: - IBActions @IBAction func showAlertTapped(sender: AnyObject) { showAlert() } // MARK: - Internal func showAlert() { if iosVersion >= 8 { var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) // The order in which we add the buttons matters. // Add the Cancel button first to match the iOS 7 default style, // where the cancel button is at index 0. alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in self.handelCancel() })) alert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in self.handelConfirm() })) presentViewController(alert, animated: true, completion: nil) } else { var alert = UIAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Confrim") alert.show() } } func handelConfirm() { println("Confirm tapped") // Your code } func handelCancel() { println("Cancel tapped") // Your code } // MARK: - UIAlertViewDelegate func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { if buttonIndex == 0 { handelCancel() } else { handelConfirm() } } }