ios - 在第一个 Controller 发生事件时以编程方式返回到第一个 Controller

标签 ios swift

<分区>

在我的 Swift 项目中,ViewController #1 在表格中列出了目的地路线。用户正在开车,我们会跟踪用户在哪里使用内置的 CLLocation 东西。但是用户现在已经从 ViewController #1 导航到应用程序的其他部分,现在在 ViewController #2 中。我在 ViewController #1 中跟踪的某个事件触发(在我的示例中;用户已到达他列表中的下一个目的地)。我需要应用程序在该事件触发时立即返回到 ViewController #1。当用户在 ViewController #2 中时如何感知?

最佳答案

NSNotificationCenter 和 Unwind Segue 似乎是可行的方法,这样可以重用此功能,您需要做的就是在相关 View Controller 上注册给定的通知。

类似的东西:

class ViewControllerOne: UIViewController {

    let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"

    //- your event happens
    SomethingHappened {
        NSNotificationCenter.defaultCenter().postNotification(CallForUnwindSegue, object: nil)
    }
}


class ViewControllerTwo: UIViewController {

    let CallForUnwindSegue = "com.yourIdentifier.unwindToVCOne"

    override func viewDidLoad() {
        super.viewDidLoad()

    //- Subscribe for notifications
        NSNotificationCenter.defaultCenter().addObserver(self, #selector(self.doSomething), name: CallForUnwindSegue)
    }

    //- Unsubscribe for notifications
    override func deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func doSomething(notification: NSNotification) {
        //Handle your event here.
    }
}

有一篇很好的 unwind segue 文章 here

关于ios - 在第一个 Controller 发生事件时以编程方式返回到第一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37689437/

相关文章:

swift - 从字符串日期中获取另一种格式的字符串

ios - 如何实现导航 Controller 的全屏滑动/平移功能?

ios - 如何通过快速单击按钮功能调用标签栏 View Controller

ios - 我是否需要使用 Mac App Store 上提供的最新可用版本的 Xcode 来构建我的项目以供提交,还是我可以使用旧版本

ios - El Capitan + Xcode 7.1 不允许我进行企业签名

ios - 为什么通过保存 PFUser 对象来保存 PFRelation?

ios - 在 swift 中使用 objc_setAssociatedObject 时提前调用 dealloc/deinit

swift - 如果用户已经评级,则禁用从用户处获取应用程序评论

ios - 访问在 UITableView 上不可见的单元格

ios - iPhone 断开连接时如何在 iWatch 上显示本地通知?