UIAlertController - action failed if warning is disabled for the first time

In the project I'm working on, I had to write a UIAlert helper module that will display pop-ups here and there in my iOS application. Pop-ups are written as class functions, which I can just call anywhere in the code (the class is static and therefore all functions).

Now I encounter a very strange error if if you release the warning once and then open it again, its actions will no longer work (as in the case, the action handler is not called). It works if you click the action the first time a popup appears, though ...

Here is the code for a specific popup for which this error (no other popups are affected at all):

static func popSkipWalkthrough() { let alert = UIAlertController(title: "Skip", message: "whatever", preferredStyle: .Alert) alert.addAction(cancelAction) alert.addAction(skipWalkthroughAction) appDelegate.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil) } 

skipWalkthroughAction defined as follows:

 static let skipWalkthroughAction = UIAlertAction(title: "Continue", style: .Default, handler: { (action: UIAlertAction!) -> Void in appDelegate.setWindowViewTo("NavCtrl", navigateTo: false) CallIn.Settings.didWalkthrough = true }) 

And cancelAction is defined as:

 static let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 

This popup, in particular, is displayed every time you click the skip button in the last step of the passage ...

I have tried some tips on what is the reason for this behavior, and I think this could have something to do with the popup that is not really getting released, but I'm not at all sure about it ...

Any ideas?

+5
source share
1 answer

That's all, although I have problems with how this reusable part is encoded, this problem can be solved by sending a copy: message to skipWalkthroughAction . Just do:

 static func popSkipWalkthrough() { let alert = UIAlertController(title: "Skip", message: "whatever", preferredStyle: .Alert) alert.addAction(cancelAction.copy() as! UIAlertAction) alert.addAction(skipWalkthroughAction.copy() as! UIAlertAction) appDelegate.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil) } 

This should solve the problem.

You can also solve this problem by moving the alert level to the instance level. You do not need to send copy: then.

Best approach

If you need the “truly” reusable experience of the UIAlertController , you better create the UIViewController extension. I have this in one of my projects:

 extension UIViewController { func showAlertControllerWithTitle(title:String?,message:String?,actions:[UIAlertAction],dismissingActionTitle:String?, dismissBlock:(() -> ())?) -> UIAlertController { let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) if dismissingActionTitle != nil { let okAction = UIAlertAction(title: dismissingActionTitle, style: .Default) { (action) -> Void in dismissBlock?() alertController.dismissViewControllerAnimated(true, completion:nil) } alertController.addAction(okAction) } for action in actions { alertController.addAction(action) } self.presentViewController(alertController, animated: true, completion:nil) return alertController } } 
+4
source

Source: https://habr.com/ru/post/1242613/


All Articles