ios - self.navigationController?.popViewControllerAnimated 来自 UIAlertController

标签 ios xcode swift uinavigationcontroller uialertcontroller

我是 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 语句避免了这种情况。

此行为记录在 Implicit Returns from Single-Expression Closures

关于ios - self.navigationController?.popViewControllerAnimated 来自 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26761835/

相关文章:

ios - 从 Nib 加载 UIView,手动调用 autolayout

iphone - 如何创建自定义 UIDatePicker 控件

ios - IPv6 - Apple 拒绝 iOS 应用程序,因为不支持 IPv6 DNS64/NAT64 网络

ios - 从文本字段输入迭代 for 循环 X 次

iphone - 删除标准 UIImagePickerController 中的照片/视频开关,以强制用户只拍照

ios - 通用链接在 iOS 11.2 中损坏

ios - 如何在iOS中从String的服务器响应中以数字格式显示新行

xcode - 如何让 Xcode 不关心从自定义脚本 (/dev/null) 返回的错误?

ios - 按设备重置日期我的应用程序会发生什么情况?

ios - 导航栏上没有后退按钮