Posted By

rossgibson on 10/15/14


Tagged


Versions (?)

UIAlertView - iOS 7 and iOS 8


 / Published in: Other
 

  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UIAlertViewDelegate {
  4.  
  5. let iosVersion = NSString(string: UIDevice.currentDevice().systemVersion).doubleValue
  6.  
  7. // MARK: - IBActions
  8.  
  9. @IBAction func showAlertTapped(sender: AnyObject) {
  10. showAlert()
  11. }
  12.  
  13. // MARK: - Internal
  14.  
  15. func showAlert() {
  16.  
  17. if iosVersion >= 8 {
  18. var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
  19.  
  20. // The order in which we add the buttons matters.
  21. // Add the Cancel button first to match the iOS 7 default style,
  22. // where the cancel button is at index 0.
  23. alert.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in
  24. self.handelCancel()
  25. }))
  26.  
  27. alert.addAction(UIAlertAction(title: "Confirm", style: .Default, handler: { (action: UIAlertAction!) in
  28. self.handelConfirm()
  29. }))
  30.  
  31. presentViewController(alert, animated: true, completion: nil)
  32. } else {
  33. var alert = UIAlertView(title: "Title", message: "Message", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Confrim")
  34.  
  35. alert.show()
  36. }
  37.  
  38. }
  39.  
  40. func handelConfirm() {
  41. println("Confirm tapped")
  42.  
  43. // Your code
  44. }
  45.  
  46. func handelCancel() {
  47. println("Cancel tapped")
  48.  
  49. // Your code
  50. }
  51.  
  52. // MARK: - UIAlertViewDelegate
  53.  
  54. func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
  55. if buttonIndex == 0 {
  56. handelCancel()
  57. } else {
  58. handelConfirm()
  59. }
  60. }
  61.  
  62. }

Report this snippet  

You need to login to post a comment.