0

I get this warning sometimes when calling UIAlert

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior ()

code used:

    func alert (dictKey: String){
        print(dictKey)
        let alertController = UIAlertController(title: nil, message: promptsArr[dictKey], preferredStyle: .Alert )
        let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

        }
        alertController.addAction(OKAction)
        if self.presentedViewController == nil {
            delay(1.0){
                self.presentViewController(alertController, animated: true, completion: nil)
            }
        } else {
            self.presentedViewController!
        }
Jeremy Andrews
  • 697
  • 10
  • 15
  • Where and how are u calling this method? – Nishant Nov 14 '15 at 06:48
  • From within this switch code: func resultOfValidation (dictKey: String, tag: Int) { delay(0.3){ switch dictKey { case "SendVeriCode": //SMS sent //Server code will have to search for previous entry of the cell no under different name _ = self.alert(dictKey) if (tag < 6){self.VerifyInput[tag + 2].becomeFirstResponder()} case "Cancel": _ = self.alert(dictKey) self.VerifyInput[tag - 1].text = "" self.VerifyInput[tag].text = "" self.VerifyInput[tag - 2].becomeFirstResponder() – Jeremy Andrews Nov 14 '15 at 07:45
  • Please add that code as well to the question. – Nishant Nov 14 '15 at 07:46

1 Answers1

2
  1. Remove delay()

  2. Put the logic inside self.presentedViewController: (no need to create the alert if it exists)

    func alert (dictKey: String){
        if self.presentedViewController == nil {
            let alertController = UIAlertController(
                title: nil,
                message: promptsArr[dictKey],
                preferredStyle: .Alert )
            let OKAction = UIAlertAction(title: "OK", style: .Default) {
                (action) in
            }
            alertController.addAction(OKAction)
    
            self.presentViewController(
                alertController,
                animated: true,
                completion: nil)
        }
    }
    
SwiftArchitect
  • 43,382
  • 25
  • 129
  • 168
  • Thank you will try it straight away. – Jeremy Andrews Nov 14 '15 at 07:41
  • Worked perfectly thanks. The delay(1.0){ } called a very useful method for synchronising two simultaneous running processes could also be used to replace OK in the code with a specific time delay for clearing the alert - refer to /* A handy bit of code created by Matt Neuburg, author of a lot of books including iOS Programming Fundamentals with Swift (O'Reilly 2015). See his reply in Stack Overflow for details: http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861 */ Where I got this from – Jeremy Andrews Nov 16 '15 at 05:53
  • http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861 is a very different situation than what you may need here. `delay` is not in any way a reliable approach for sync. Look into `-performSelector:afterDelay`, semaphores or, if at all possible, simple completion block. – SwiftArchitect Nov 16 '15 at 22:34
  • Thanks for that will have a look at these suggestions – Jeremy Andrews Nov 18 '15 at 04:09
  • Thanks for answers - hopefully my reformatting will remove the down vote – Jeremy Andrews Jul 16 '17 at 10:27