× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

self.navigationController?.popViewControllerAnimated from UIAlertController

Antonio Bello
Mar 01, 2015
<p>Just add an explicit <code>return</code> statement in the closure body:</p> <pre><code>alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in self.navigationController?.popViewControllerAnimated(true) return })) </code></pre> <p>The reason why that happens is that a single statement closure is handled as the return value, so the compiler uses the return value of <code>popViewControllerAnimated</code>, which unsurprisingly is a <code>UIViewController?</code>. The explicit return statement avoids that.</p> <p>This behavior is documented in <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-XID_157">Implicit Returns from Single-Expression Closures</a></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/26761835/self.navigationController?.popViewControllerAnimated%20from%20UIAlertController/26761887">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus