Egle Matutyte
Egle Matutyte

Reputation: 225

swift 3 make alert with action button and present controller

everyone I am making Alert from my AlertController, but I want after action button "OK" is pressed, open LoginController, how I can do that?I tried to do that just like below, but it's not working

AlertController:

class AlertController: NSObject {

class func showErrorWith(title:String? = nil, message:String? = nil, controller: UIViewController , complition:(() -> ())?){
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in


//            let storyboard = UIStoryboard(name: "Main", bundle: nil)
//            let viewController = storyboard.instantiateViewController(withIdentifier :"LoginVC")
//            self.present(viewController, animated: true)
//            

    }))
    controller.present(alert, animated: true, completion: nil)
}

Upvotes: 1

Views: 3572

Answers (2)

Chanchal Warde
Chanchal Warde

Reputation: 983

func ShowAlert(vc: UIViewController, title: String, message:String, affirmButton: String = "OK", cancelButton: String = "Cancel", onAffirmation: (Void) -> (Void) = { })
{
    let alertView = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    alertView.addAction(UIAlertAction(title: affirmButton, style: UIAlertActionStyle.Default, handler:{(action) in
        onAffirmation()
    }))
    alertView.addAction(UIAlertAction(title: cancelButton, style: UIAlertActionStyle.Default, handler: nil))
    vc.presentViewController(alertView, animated: true, completion: nil)
}

Upvotes: 2

Alexandr Chernyy
Alexandr Chernyy

Reputation: 56

It's happen because you call self.present in this code self - It's kind of NSObject. Just call controller.present(viewController, animated:true);

Upvotes: 4

Related Questions