我有一个使用 http 调用从外部存储传输视频的应用程序。 当用户的设备未连接到网络服务时,我需要应用程序返回到之前的 Controller 。
流程如下:用户访问元素列表(表格 View 单元格),选择一个,然后应用程序转到播放器 Controller 。在此 Controller 上,进行调用以流式传输文件。
我在 Controller 外部的类中使用 API 调用处理程序,但我不知道如何继续使其从此处返回到上一个 Controller (元素列表)。
连接问题错误全部在 api 类中捕获。 我没有显示任何代码,因为我不确定它是否相关。如果您需要查看任何内容,请告诉我,我将更新问题。应该采取什么方法来做到这一点? (当然我使用导航 Controller )
谢谢
最佳答案
如果你想返回到之前的 View Controller ,你应该使用:
navigationController?.popViewControllerAnimated(true)
如果您需要不在 View Controller 中而是在另一个类中使用此功能,您可以使用 NSNotificationCenter 在需要显示前一个 Controller 时通知 View Controller ,就像这样:
你的ViewController
override func viewDidLoad()
{
...
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "goBack:",
name: "goBackNotification",
object: nil)
...
}
func goBack(notification: NSNotification)
{
navigationController?.popViewControllerAnimated(true)
}
另一个类
NSNotificationCenter.defaultCenter().postNotificationName("goBackNotification", object: nil)
不要忘记删除 YourViewController 中的观察者:
deinit
{
NSNotificationCenter.defaultCenter().removeObserver(self)
}
编辑1:显然,您可以使用委托(delegate)而不是 NSNotification 方法。如果你不知道 NSNotification 和 delegate 之间的区别我推荐给你 this answer .
关于ios - 从 swift 类返回到上一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380033/