Return to Snippet

Revision: 67678
at October 15, 2014 20:23 by rossgibson


Initial Code
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()
        }
    }

}

Initial URL

                                

Initial Description

                                

Initial Title
UIAlertView - iOS 7 and iOS 8

Initial Tags

                                

Initial Language
Other