2017-02-20 5 views
1

все, что я делаю Alert от моего AlertController, но я хочу, чтобы после нажатия кнопки «ОК» нажата, откройте LoginController, как я могу это сделать? Я пытался это сделать так же, как ниже, но это не работаетswift 3 make alert с кнопкой действия и текущим контроллером

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) 
} 
+0

вам нужно переместить настоящий Кодекс в части Содержащаяся. –

ответ

4

это произойдет потому, что вы называете self.present в этом коде я - это своего рода NSObject. Просто позвоните controller.present(viewController, animated:true);

+0

Спасибо, он работает! –

+0

У меня возник вопрос: если я не на LoginController, а на MainController, и я вызываю ту же функцию и получаю ошибку, но предупреждение не отображается, как сделать отображение предупреждений независимо от того, какой контроллер выполняется? –

2
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) 
} 
Смежные вопросы