self.navigationController?.popViewControllerAnimated from UIAlertController [英] self.navigationController?.popViewControllerAnimated from UIAlertController

查看:27
本文介绍了self.navigationController?.popViewControllerAnimated from UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 swift 的新手,但我想我已经掌握了它.但这阻碍了我的进步.

I'm new to swift but I think I'm getting a hang of it. This stumped my progress pretty hard though.

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

What I want to do is to throw an error message to the user when we can't find relevant data to his query, and then proceed to take him back to the previous ViewController.

但是,我在执行此操作时遇到了麻烦.在添加操作的行中,我收到以下错误:UIViewController?"不是 Void 的子类型

However, I'm having real trouble doing this. On the line where I add the action I get the following error: 'UIViewController?' is not a subtype of 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,但还是不明智.

How do I do this? Am I missing something obvious? I tried messing around with the deprecated UIAlertView but became none the wiser.

推荐答案

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

Just add an explicit return statement in the closure body:

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

发生这种情况的原因是单条语句闭包作为返回值处理,所以编译器使用了popViewControllerAnimated的返回值,不出所料,它是一个UIViewController?.显式 return 语句避免了这种情况.

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 popViewControllerAnimated, which unsurprisingly is a UIViewController?. The explicit return statement avoids that.

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

这篇关于self.navigationController?.popViewControllerAnimated from UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆