不,如果两个 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 ....
}