self.navigationController?来自UIAlertController的.popViewControllerAnimated

Joa*_*edt 7 xcode uinavigationcontroller ios swift uialertcontroller

我是新手,但我想我已经掌握了它.这让我的进步很难过.

我想要做的是当我们无法找到他的查询的相关数据时向用户抛出错误消息,然后继续将他带回到之前的ViewController.

但是,我在这方面遇到了麻烦.在我添加操作的行上,我收到以下错误:'UIViewController?' 不是Void的子类型

let alertController = UIAlertController(title: "Oops", message: "We couldn't find any data for this title, sorry!", preferredStyle: UIAlertControllerStyle.Alert)

alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
    self.navigationController?.popViewControllerAnimated(true)   
}))
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我错过了一些明显的东西吗 我试着搞砸了被弃用的UIAlertView,但却没有变得更聪明.

Ant*_*nio 24

只需return在闭包体中添加一个显式语句:

alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
    self.navigationController?.popViewControllerAnimated(true)
    return
}))
Run Code Online (Sandbox Code Playgroud)

发生这种情况的原因是单个语句闭包被处理为返回值,因此编译器使用返回值popViewControllerAnimated,这不足为奇UIViewController?.显式返回语句避免了这种情况.

单表达式闭包的隐式返回中记录了此行为