Evan Pu
Evan Pu

Reputation: 2187

ios swift reactive programming

Not sure the best way to phrase the question, but I'll give it a try. I have a button, whose visibility is directly tied to a state variable in my model. When that state variable is true, I want the button to be visible, and when that state variable is false, I want the button to be invisible.

Is there a native way in swift that ties the rendering of a button directly to a state variable? My work around is creating a function, toggle state, that ties the state change and the button visibilities together, but it really seems the button visibility ought to be reactive on the state.

Upvotes: 1

Views: 322

Answers (1)

Eyeball
Eyeball

Reputation: 3307

Given that you have a reference to your view from your model, perhaps via an interface, you can define your state variable like this:

var stateVar: Bool {
   didSet {
      // note that we are referencing our own variable here.
      self.delegate.setButtonHidden(self.stateVar)  
   }
}

I'd recommend using the framework ReactiveCocoa though. It supports what you are trying to do, without interfacing.

Upvotes: 2

Related Questions