2017-04-27 5 views
1

Ich versuche einfach Alarm in neuem Xcode Update 8.3.2 i ist vor Problem zu tun, während die Warnung Dialog präsentiert:Kann der Wert des Typs 'UIAlertAction' nicht in den erwarteten Argumenttyp 'UIViewController' konvertiert werden?

@IBAction func testButonAlert() 
{ 

    let alertAction = UIAlertAction(title : "Hi TEst" , 
            style : UIAlertActionStyle.destructive, 
            handler : { (UIAlertActionStyle) -> Void in print("") }) 

    self.present(alertAction , animated: false, completion: nil) 
} 

Fehler:

nicht Wert vom Typ 'UIAlertAction' umwandeln kann erwartete Argument Typ 'UIViewController'

enter image description here

Antwort

2

Sie werden die action präsentieren, die unmöglich ist (und bewirkt, dass der Fehler).

Sie benötigen einen Elternteil UIAlertController die Aktion beispielsweise zur Befestigung:

@IBAction func testButonAlert() 
{ 
    let alertController = UIAlertController(title: "Hi TEst", message: "Choose the action", preferredStyle: .alert) 
    let alertAction = UIAlertAction(title : "Delete Me" , 
            style : .destructive) { action in 
             print("action triggered") 
            } 

    alertController.addAction(alertAction) 
    self.present(alertController, animated: false, completion: nil) 
} 
2

Sie können einfach erstellen Benachrichtigungen über UIAlertController:

let yourAlert = UIAlertController(title: "Alert header", message: "Alert message text.", preferredStyle: UIAlertControllerStyle.alert) 
yourAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (handler) in 
         //You can also add codes while pressed this button 
        })) 
self.present(yourAlert, animated: true, completion: nil) 
3

Sie UIAlertController verwenden sollten.

let alertVC = UIAlertController(title: "title", message: "message", preferredStyle: .alert) 
alertVC.addAction(UIAlertAction(title: "Close", style: .default, handler: nil)) 
self.present(alertVC, animated: true, completion: nil) 
0

Für Swift 4,0

class func showAlert(_ title: String, message: String, viewController: UIViewController, 
         okHandler: @escaping ((_: UIAlertAction) -> Void), 
         cancelHandler: @escaping ((_: UIAlertAction) -> Void)) { 
     let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     let OKAction = UIAlertAction(title: "OK", style: .default, handler: okHandler) 
     alertController.addAction(OKAction) 
     let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: cancelHandler) 
     alertController.addAction(cancelAction) 
     viewController.present(alertController, animated: true, completion: nil) 
    } 
Verwandte Themen