2

In Swift UIAlertView is deprecated and replaced with UIAlertController. As I see the only way to show a UIAlertController is through a UIViewController, but sometimes you want to show it from a lets say UIView.

Is this possible now with IOS8 and Swift?

tashuhka
  • 5,028
  • 4
  • 45
  • 64
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • 2
    The normal way to handle this would be to use a delegate. Having your view pop up an alert seems like a MVC violation. – sapi Aug 30 '14 at 11:41
  • Did you see this: http://stackoverflow.com/questions/24584364/how-to-create-an-alert-in-a-subview-class-in-swift – ricardopereira Aug 30 '14 at 12:44
  • http://stackoverflow.com/questions/25505045/display-uialertcontroller-from-uiview-nsobject-class – Jageen Sep 05 '14 at 04:07

2 Answers2

1

UIAlertView is still available in Swift, it's only deprecated in iOS 7. If you want to use the latest iOS 8 API I would recommend doing something like below. Although for simplicity if you're targeting iOS 7 and iOS 8 I would recommend using the standard UIAlertView only and not implementing the UIAlertController.

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

}

Ross Gibson
  • 980
  • 1
  • 8
  • 14
0
        class MyViewController: UIViewController {
@IBOutlet var myUIView: MyUIView
}

        class MyUIView: UIView {
func showAlert() {

        let parentViewController: UIViewController = UIApplication.sharedApplication().windows[1].rootViewController

        var alert = UIAlertController(title: "Title", message: "message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in

            println("clicked OK")
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Destructive, handler: {(action: UIAlertAction!) in

            println("clicked Cancel")

        }))
        parentViewController.presentViewController(alert, animated: true, completion: nil)

}
        - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
        {           
        }
pravin salame
  • 349
  • 3
  • 4