devxoul / Then

✨ Super sweet syntactic sugar for Swift initializers
MIT License
4.18k stars 290 forks source link

How to use this syntax for IBOutlet views? #3

Closed dinarajas closed 8 years ago

dinarajas commented 8 years ago

Instead of creating a new instance for a view, is there a possibility to fetch the existing instance and set attributes to it? I want to update attributes initially for IBOutlets views.

RuiAAPeres commented 8 years ago

@dineshrajas you can use it with your IBOutlets, no new instance is created. Have a look at how then is implemented:

    func then(block: Self -> Void) -> Self {
        block(self)
        return self
    }

With Outlets you can simply:

self.myOutlet.then { // do stuff }
devxoul commented 8 years ago

@dineshrajas, thank you for asking. Since IBOutlet variables are initialized lately, you cannot use then with the property declarations. However, as @RuiAAPeres mentioned, you can use then just after the IBOutlets are created.

ilyapuchka commented 8 years ago

@dineshrajas for outlets it is very nice to use didSet property observer for that purpose. It will be called every time you set the property. First time it will be called by the system when instantiating view from xib/storyboard.

@IBOutlet weak var button: UIButton! {
  didSet {
    //set button attributes
  }
}
AndrewSB commented 8 years ago

+1 on @ilyapuchka's solution, that's what I've been doing too. This can probably be closed - I don't think we're going to get anything better than that anytime soon