self.navigationController?来自UIAlertController的.popViewControllerAnimated

我是新手,但我觉得我已经掌握了它。 尽管如此,这让我的进步很难过。

我想要做的是当我们无法找到他的查询的相关数据时向用户抛出错误消息,然后继续将他带回到之前的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) })) 

我该怎么做呢? 我错过了一些明显的东西吗 我试着搞乱了不赞成使用的UIAlertView,但却没有变得更聪明。

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

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

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

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