2016-04-29 2 views
0

Я новичок в быстром, я хочу отклонить предупреждение, которое присутствует на экране , когда появляется новое предупреждение.Отказ старого UIAlertViewController перед тем, как представить новый UIAlertViewController

Я пробовал:

func showDefaultAlert(controller: UIViewController, title: String, message: String) { 

    // create the alert 
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) 

    // add an action (button) 
    alert.addAction(UIAlertAction(title: defaultTextForNormalAlertButton, style: UIAlertActionStyle.Default, handler: nil)) 

    // show the alert 
    //controller.presentViewController(alert, animated: true, completion: nil) 

    self.showAlert(controller, alert: alert) 

} 

func showAlert(controller: UIViewController, alert: UIAlertController) { 

     if let currentPresentedViewController = UIApplication.sharedApplication().keyWindow?.rootViewController?.presentedViewController { 
      if currentPresentedViewController.isKindOfClass(UIAlertController) { 
       currentPresentedViewController.dismissViewControllerAnimated(false, completion: { 
        controller.presentViewController(alert, animated: true, completion: nil) 
       }) 
      }else { 

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



// Call to above method in view controller class: 

SPSwiftAlert.sharedObject.showDefaultAlert(self, title:"Title1", message1: "Message") 

SPSwiftAlert.sharedObject.showDefaultAlert(self, title:"Title2", message: "Message2") 

- но приведенный выше код дает ошибку времени выполнения, как:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UIAlertController: 0x7fceb95dcfb0>) 
+0

Что такое "контроллер"? в контроллере.presentViewController (alert, ... –

+0

проверить сейчас, я добавил весь код. –

+1

Вы не пытаетесь загрузить то же предупреждение, которое вы только что уволили? – 4oby

ответ

0

После предъявления UIAlertController вы можете проверить его видимость, как показано ниже:

Представлено UIAlertController:

let alerts=UIAlertController(title: "Test", message: "Test", preferredStyle: .Alert) 

presentViewController(alerts, animated: true, completion: nil) 

Проверить, если UIAlertController видна:

if (alerts.isViewLoaded()) 
{ 
     print("Visible") 
     //Here you can dismiss the controller 
     //dismissViewControllerAnimated(true, completion: nil) 
} 

Проверьте эту демо: Source code

+0

Как я мог уволить, если присутствует предупреждение? –

+0

Просто раскомментируйте строка, добавленная в приведенный выше код. - увольнениеПросмотрСообщение (true, завершение: ноль) – Rahul

+0

не работает в моем случае :( –

Смежные вопросы