Question

J'utilise Swift et j'ai une classe appelée ViewController() qui est liée à mon storyboard.J'essaie d'afficher une vue d'alerte d'une autre classe.J'utilise le code suivant mais il n'ouvrira pas de boîte d'alerte.Des idées?

public class SomeClass {

        func showAlert(title:String, body:String) {
            var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

}
Était-ce utile?

La solution

    func showAlert(title:String, body:String) {
        var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))        
        var vc: ViewController = ViewController()
        vc.presentViewController(alert, animated: true, completion: nil)
    }

Autres conseils

Tu dois appeler presentViewController Sur ton UIViewController et passe dans ton UIAlertViewController.

Dans UIViewController :

self.presentViewController (alerte, animée :vrai, achèvement :néant)

alert étant ton UIAlertViewController.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top