Mike Strong
Mike Strong

Reputation: 940

how do I listen for a change of value in a variable using swift?

I have a string type variable thats value is updated via a PickerView, and when that value gets updated, it then updates the title of a button, like so:

@IBOutlet weak var selectedCountry: UIButton!

 var pickedCountry: String?

func checkForPickedCountry() {
        if self.pickedCountry != nil {
            selectedCountry.setTitle("\(pickedCountry!)", forState: .Normal);
        } else {
            selectedCountry.setTitle("Tap here pick a country", forState: .Normal);
        }

UPDATE

Now like i mentioned, I'm using a pickerView to update the value of pickedCountry, and when a country is selected from the pickerView, it then displays a second button titled selectedFlag that shoots another pickerView to choose a country flag. Now the reason I want to listen to the 1st pickedCountry var for change is so I can change the title of the selectedFlag button to the default.

So here is the final code I have:

@IBOutlet weak var selectedCountry: UIButton!
@IBOutlet weak var selectedFlag: UIButton!

 var pickedCountry: String?
 var pickedFlag: String?

func checkForPickedCountry() {
        if self.pickedCountry != nil {
            selectedCountry.setTitle("\(pickedCountry!)", forState: .Normal);
            selectedFlag.hidden = false
        } else {
            selectedCountry.setTitle("Tap here pick a country", forState: .Normal);
            selectedFlag.hidden = true
        }


       if self.pickedFlag != nil {
           selectedFlag.setTitle("\(pickedCountry!)", forState: .Normal);
       } else {
           selectedFlag.setTitle("Tap here to pick a flag", forState: .Normal)
       }

Now how do i set selectedFlag's title to "Tap here to picka flag when the value of `PickedCountry is changed??

Upvotes: 1

Views: 11247

Answers (3)

Radu Nunu
Radu Nunu

Reputation: 134

You can implement willSet in your variable to call your function everytime when variable is changing. Also you can modify your function to get new value like this:

var pickedCountry: String? {
    willSet(newValue) {
      guard let oldValue = str, new = newValue else { return }
      if oldValue != new { //....Your value is changed }
    }
}

func checkForPickedCountry(updatedValue: String) {...}

Upvotes: 2

CodeBender
CodeBender

Reputation: 36620

You can monitor it with a key value observer.

In the class that is supposed to look for the changes, do the following:

Add the observer

func addObservers() {
    let observerKeyLocation = "pickedConuntry"
    classThatMonitors.addObserver(self, forKeyPath: observerKeyLocation, options: .New, context: nil)
}

Then, override the observerValueForKeyPath function:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == observerKeyLocation {
        if self.pickedCountry != nil {
            selectedCountry.setTitle("\(pickedCountry!)", forState: .Normal);
        } else {
            selectedCountry.setTitle("Tap here pick a country", forState: .Normal);
        }
    }
}

Now, whenever that value is changed, your observer will immediately be updated and can call the code you place inside it.

Just remember, if you deinit the class being observed, you must also remove the observer to avoid a crash.

Upvotes: 2

JAck
JAck

Reputation: 854

You can use simple logic as Below::

Set one Int Variable var flag : Int = 0 Now In whole program flag has 0 value.

Again when you tap Button change its value to 1 by self.flag = 1. Again when you tap second one make it 0 by self.flag = 0.

By getting the value of that flag variable you will listen the event.

You can Do same for more then two by providing values 2, 3, 4... To flag variable

Upvotes: 1

Related Questions