【发布时间】:2017-03-10 19:17:13
【问题描述】:
我有一个按钮,其发送者会观察通知并在触发后更改其标题。在触发之前,按钮标题为“A”。触发后,按钮标题为“B”。
我正在尝试将更改的状态保存在 Realm 中,以便用户可以看到按钮已更改。
发布(触发)通知发生在另一个添加 UIView 的按钮操作中。该子视图拥有发布通知:
NotificationCenter.default.post(name: fireNotification, object: nil)
当帖子被触发时,另一个添加(观察者)通知会作用于这个事件:
// MARK: - Observe Notification
NotificationCenter.default.addObserver(forName: fireNotification, object: nil, queue: nil) { notification in
print("Notification Observed")
sender.setTitle("B", for: .normal)
sender.setTitleColor(.red, for: .normal)
sender.isEnabled = false
}
这显然将按钮状态更改为 B。我如何编写并将其保存到我的领域?我试过了:
// MARK: - Observe Notification
NotificationCenter.default.addObserver(forName: fireNotification, object: nil, queue: nil) { notification in
print("Notification Observed")
let realm = try! Realm()
try! realm.write {
sender.setTitle("B", for: .normal)
sender.setTitleColor(.red, for: .normal)
sender.isEnabled = false
}
}
我应该在对象参数中添加一些东西吗?
【问题讨论】:
标签: ios notifications swift3 uibutton realm