Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissal of UIAlertController (best practice)

When using UIAlertController like this:

var alert = UIAlertController(title: "Core Location", 
     message: "Location Services Disabled!", 
     preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, 
     handler: nil))
self.navigationController.presentViewController(alert, animated: true, 
     completion: nil)

I noticed that the dismissal of the alert view is seemingly done automatically. Shouldn't the dismissal of a presented ViewController be done by the presenting ViewController via a delegate call?

like image 467
Woodstock Avatar asked Aug 17 '14 12:08

Woodstock


1 Answers

Is some Cases you may like to use this:

  class MyAlertController : UIAlertController {
    var dismissHandler : (()->())?
    override func viewDidDisappear(_ animated: Bool) {
      dismissHandler?()
      super.viewDidDisappear(animated)
    }
  }

Usage:

  let alert = MyAlertController(title: ...
  let cancelButton = UIAlertAction(titl
  alert.dismissHandler = { /*...do something */ }
  alert.addAction(cancelButton)
  ...
like image 167
bobln Avatar answered Oct 18 '22 15:10

bobln