ios - 从 swift 类返回到上一个 Controller

标签 ios swift class uinavigationcontroller

我有一个使用 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/

相关文章:

ios - 位置管理器 :didFailWithError: not called if user Location Services are off

ios - 映射解析对象在 callFunctionInBackground 中不起作用

ios - 在 Swift 中将路径扩展名转换为小写

ruby - 没有继承的人如何覆盖类方法并从新方法中调用原始方法?

ios - 在 iPhone 应用程序中将图像加载到内存中

objective-c - 为什么我的字符串在我的 iOS 应用程序中可能不安全?

javascript - 类中的jquery函数混淆了this关键字

c++ - 尝试在 C++ 中的 vs 代码中运行分离的类

ios - 后台线程中的地址簿 : weird intermittent crash

ios - 将模型发布到委托(delegate) View 的正确方法?