title: use activity indicator in many VC without duplicating code swift
body: <p>I have two <code>ViewControllers</code> (A and B) in swift IOS. Both A and B loads data from internet (separately). I want to display <code>activityIndicator</code> while loading. I know I can do it the bad way by declaring it once in each VC as follows</p>
  
  <p><strong>ViewController A</strong></p>
  
  <pre><code>var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  
  func activityIndicatorBegin() {
      activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
      activityIndicator.center = self.view.center
      activityIndicator.hidesWhenStopped = true
      activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
      view.addSubview(activityIndicator)
      activityIndicator.startAnimating()
      disableUserInteraction()
  
      greyView = UIView()
      greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
      greyView.backgroundColor = UIColor.blackColor()
      greyView.alpha = 0.5
      self.view.addSubview(greyView)
  }
  
  func activityIndicatorEnd() {
      self.activityIndicator.stopAnimating()
      enableUserInteraction()
      self.greyView.removeFromSuperview()
  }
  </code></pre>
  
  <p>and do the exact same for ViewController B and call the activityIndicatorBegin and activityIndicatorEnd functions that is declared in B view controller. However, I want to make the code cleaner. How could it be done? I am trying to make code cleaner these days.</p>
  
  <p>Thanks, </p>
  
  <p>--UPDATE--</p>
  
  <p>I would imageing something like the following code would work. But it doesnt because I cant declare variables in extensions</p>
  
  <pre><code>    var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  
  extension UIViewController {
      func activityIndicatorBegin() {
      activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
      activityIndicator.center = self.view.center
      activityIndicator.hidesWhenStopped = true
      activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
      view.addSubview(activityIndicator)
      activityIndicator.startAnimating()
      disableUserInteraction()
  
      greyView = UIView()
      greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
      greyView.backgroundColor = UIColor.blackColor()
      greyView.alpha = 0.5
      self.view.addSubview(greyView)
  }
  
  func activityIndicatorEnd() {
      self.activityIndicator.stopAnimating()
      enableUserInteraction()
      self.greyView.removeFromSuperview()
      }
  }
  </code></pre>