我是 swift 的新手,但我想我已经掌握了窍门。不过,这严重阻碍了我的进步。
我想做的是当我们找不到与他的查询相关的数据时,向用户抛出一个错误信息,然后继续将他带回之前的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?
。显式 return 语句避免了这种情况。
关于ios - self.navigationController?.popViewControllerAnimated 来自 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26761835/