私はその変数にdidSet値オブザーバを作成します。任意のViewControllerが表示されたら、デリゲートをselfに設定します。 ViewControllerでその変数の変更を追跡します。
それぞれのViewControllerを行う必要があります。
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
Singleton.shared.delegate = self
}
}
extension ViewController: SingletonDelegate{
func variableDidChange(newVariableValue value: Int) {
//here u get value if changed
}
}
そしてシングルトンあなたの変数とプロトコル
protocol SingletonDelegate:class {
func variableDidChange(newVariableValue value:Int)
}
class Singleton {
var variable:Int = 0{
didSet{
delegate?.variableDidChange(newVariableValue: variable)
}
}
private init(){}
weak var delegate: SingletonDelegate?
static let shared = Singleton()
}
PDで:ナビゲーションの唯一のトップのViewControllerをnewValueにイベントを処理するこの方法に気づきます。
キーバリュー観測? – matt
はい。私はView1にaddObserverをしますか?これについての良い例はありますか? – Arcadian
あなたは_how_にキー値観測を依頼していますか? https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html「Key-Value観察」で検索してください。 – matt