ios - swift/xcode/iOS : Using Firebase to retrieve data already collected and displaying it in a separate view controller

标签 ios swift xcode firebase-realtime-database swift3

我正在开发一款允许您撰写和查看帖子的应用程序,有点像 Twitter 允许的那样。我使用的数据库是 Firebase,我让它在命令完成时接收信息。每次我打算接收信息时,我都会这样做,而且我可以在 Firebase 中查看所有信息。但是,我随后想在一个SEPARATE View Controller 中显示我的数据库中的信息(使用 TableView 单元格,每个帖子在不同的单元格中)。我找到了允许在该 View Controller 中查看数据库中收到的信息的教程,但是我找不到在不同的 View Controller 中执行此操作的方法。为了简化问题,我想获取我已有的数据并将其显示在与接收信息的 View Controller 不同的 View Controller 中。

最佳答案

如果您能够在该 View Controller 中收集一组 post 对象,您可以使用本地通知将数组传递给单独的 View Controller 。

数据来源:

let nc = NSNotificationCenter.defaultCenter()
//Name of notification is "postsRetrieved"
nc.postNotificationName("postsRetrieved", object:nil,userInfo:["posts":postsArray])

现在,向单独的 View Controller 添加一个监听器

let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: "postsFunction:", name: "postsRetrieved", object: nil)

最后,从您的监听器(“postsFunction”)实现选择器

func postsFunction(notification:NSNotification) {
    let userInfo:Dictionary<Array,Array!> = notification.userInfo as Dictionary<Array,Array!>
    let postsArray = userInfo["postsArray"]

    //Now, use this array as your table data source
}

关于ios - swift/xcode/iOS : Using Firebase to retrieve data already collected and displaying it in a separate view controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39083509/

相关文章:

objective-c - 如何检测是否在 OS X 上按下了某个键?

ios - 让球落入网(子节点)并做出嗖嗖的 Action ? (PhysicsBody/ARKit)

Xcode 将代码移动到新项目

iphone - UIActionSheet 按钮索引

objective-c - 音乐可视化/交互应用程序的最佳方法

ios - 如何在真实设备上将渐变应用到 iOS 10 中的 UIView

ios - UITableView自定义单元格在滚动时消失并随机出现

ios - 对具有字符串文字和 NSObject 下标类型的下标的引用不明确

iOS标签栏 Controller 在 Controller 顶部添加导航按钮

ios - 当前演示完成后如何执行segue