title: lazy var or let for UIButton
body: <p>When i want to add a <code>UIButton</code> in a view controller's view, here are the ways:</p>
  
  <p><strong>First</strong></p>
  
  <pre><code>let button: UIButton = UIButton()
  </code></pre>
  
  <p>then configure properties in <code>viewDidLoad</code> method.</p>
  
  <p><strong>Second</strong></p>
  
  <pre><code>lazy var button: UIButton = {
      let buttonTemp = UIButton()
      buttonTemp.backgroundColor = UIColor.clearColor()
      button.addTarget(self, action: "connect", forControlEvents: UIControlEvents.TouchUpInside)
      self.view.addSubview(buttonTemp)
      return buttonTemp
  }()
  </code></pre>
  
  <p><strong>Third</strong></p>
  
  <pre><code>let button: UIButton = {
          let button = UIButton(type: .Custom)
          button.backgroundColor = UIColor.greenColor()
          return button
      }()
  </code></pre>
  
  <p>My question is which way should i use or which way is better? </p>
  
  <ol>
  <li><p>I dislike the first way because i have to add an extra method to configure other properties.</p></li>
  <li><p>Second is ok for me, i just need call <code>button</code> anywhere i want to.</p></li>
  <li><p>I think use let is suit the best so i use the third way, but the thing is i can't call <code>self</code>, which is if i add this link in the closure:</p>
  
  <p>button.addTarget(self, action: "connect", forControlEvents: UIControlEvents.TouchUpInside)</p></li>
  </ol>
  
  <p>I got the error:</p>
  
  <p><code>ViewController.swift:24:26: Cannot convert value of type 'NSObject -&gt; () -&gt; ViewController' to expected argument type 'AnyObject?'</code></p>
  
  <p>So i have add this line(any line with <code>self</code>) out of this closure. Any way can solve this?</p>
  
  <p>Summary, which way is better or suit? Or any better way? thanks!</p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>When i am using Objective C, i'd like use getter in this way</p>
  
  <pre><code>- (UIButton *) button {
      if (!_button) {
          _button = [[UIButton alloc] init];
          _button.backgroundColor = [UIColor redColor];
          ...
      }
      return _button;
  }
  </code></pre>
  
  <p>so my <code>viewDidLoad</code> will be clean and looks good:</p>
  
  <pre><code>- (void) viewDidLoad {
     ...
     [self.view addSubview:self.button];
     ...
  }
  </code></pre>