【问题标题】:How to pass data between View Controllers in UITabBarController?如何在 UITabBarController 中的视图控制器之间传递数据?
【发布时间】:2021-06-06 02:20:34
【问题描述】:

我有两个问题。 我想传递数据。我有两个ViewControllers。 控制器 A 有数据,我想将它从 A 传递到 B 控制器。

首先-标签栏控制器有一个关系segue。我可以用这个吗?例如:执行 segue 还是准备?

第二 - 我想在ViewControllers 和UITabBarController 之间传递数据 我不能这样做。我搜索了这个我找不到这个...

我不知道搜索词是否错误。你能告诉我怎么做或如何搜索吗?

【问题讨论】:

  • 通过TabBarController将A的数据发送给B。

标签: ios swift uitabbarcontroller uitabbar


【解决方案1】:
  1. 没有。一段关系并不是真正的segue。仅通过切换选项卡不会触发 segue。

  2. 您只需将数据放在各种视图控制器可以找到的地方。如果您想通知其他视图控制器数据已更改,请使用通知或利用标签栏控制器委托知道用户何时切换标签栏项目这一事实。

【讨论】:

    【解决方案2】:

    不,如果两个 ViewController 连接到 TabbArController,则不能直接在两个 ViewController 之间传递数据。

    如果您愿意,您可以通过 NotificationCenter 进行管理(因此当发生新的更改并发布通知时,它会自动刷新数据)

    NotificationCenter.default.addObserver(
                self,
                selector: #selector(self.getData),
                name: .refreshAttendanceForSelectedDate,
                object: nil)
    
    @objc func getData() {
    /// perform task in view controller
    }
    

    // 收到响应后发布更改 ....

    NotificationCenter.default.post(name: Constants.Notification.refreshAttendanceForSelectedDate, object: nil)
    

    或者 您可以通过创建单例对象将 DataStorgae 更新的数据传递到相应模型类型 (LocalCache) 的变量中。此变量将驻留在内存中,直到应用程序驻留在内存中。强制杀死应用程序或从内存中清除应用程序后,该变量将自动删除。

    注意:如果需要,请不要忘记将值设置为 nil。 (例如注销操作)

    class DataStorage {
        
        /// Created the singleton objectbas
        static let instance = DataStorage()
        
       /// Created the properties
        var notificationbadgeCount: Int?
        var configData: ConfigurationModel?    
    }
    

    按组使用

    DataStorage.instance.selectedStaffId = model.id
    

    获取价值

    if let id = DataStorage.instance.selectedStaffId {
    /// Perform respective task ....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多