简体   繁体   English

Swift 如何判断共享实例中的变量何时更改

[英]Swift how to tell when a variable changed in a shared instance

I have a variable in a singleton pattern that is used/referenced through out the app.我有一个在整个应用程序中使用/引用的单例模式变量。

I have a mainVC that has a custom view in it that sets a variable.我有一个 mainVC,它有一个自定义视图,可以设置一个变量。 I want the view controller that is presented in the navigation controller to be notified when this variable changes.我希望在此变量更改时通知导航控制器中显示的视图控制器。

For example:例如:

in my mainVC it has an embedded navigation controller, which I push View1.在我的 mainVC 中,它有一个嵌入式导航控制器,我将其推入 View1。 On the mainVC toolbar, there is a textfield that the user can enter values.在 mainVC 工具栏上,有一个用户可以输入值的文本字段。 When this values changes, I would like to have View1 to know it has changed.当这个值改变时,我想让 View1 知道它已经改变了。 View1 can be any view that is currently presented. View1 可以是当前呈现的任何视图。

I would create a didSet value observer on that variable.我会在那个变量上创建一个 didSet 值观察者。 Then when any ViewController willappear, set the delegate to self.然后当任何 ViewController 出现时,将委托设置为 self。 And track the changes of that variable on the ViewController.并在 ViewController 上跟踪该变量的变化。

Each ViewController should do:每个 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
    }
}

And the Singleton with your variable and protocol以及带有变量和协议的单身人士

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: notice this way only the top ViewController in the navigation will handle the newValue event. PD:注意这种方式只有导航中的顶部 ViewController 会处理 newValue 事件。

I have used this pattern a few times and I prefer to use NotificationCenter.我已经多次使用这种模式,我更喜欢使用 NotificationCenter。

In your Singleton class you need to use Post method every time the value changes , And in your viewcontroller you would add the observer to reflect or react to the change.在您的 Singleton 类中,每次值更改时都需要使用 Post 方法,并且在您的视图控制器中,您将添加观察者以反映或对更改做出反应。

There are multiple ways to tackle this.有多种方法可以解决这个问题。 And there are plenty of great tutorials out there for the same.还有很多很棒的教程。

I wanted to add to @matt 's comment, KVO is one way to go about your solution, and from your question I think it would be the best way.我想添加到@matt 的评论中,KVO 是解决您的问题的一种方法,从您的问题来看,我认为这将是最好的方法。 You can use the "normal" KVO which is what I implement when using it that @matt linked to.您可以使用“普通”KVO,这是我在使用@matt 链接到的它时实现的。

BUT, I did find a new way that KVO can be set up that I didn't know before so I wanted to share it:但是,我确实找到了一种可以设置 KVO 的新方法,这是我以前不知道的,所以我想分享它:

var testString: String = "Hello World" {
    //After setting 🔥
    didSet {
      // Code you want to execute after a new value is set
    }

    //Prior to setting 🐶
    willSet {
      // Code you want to execute right before setting a new value
    }
}

I seen this while reading this article: KVO and Lazy Stored Properties我在阅读这篇文章时看到了这一点: KVO and Lazy Stored Properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM