title: LoadView function called twice in swift
body: <p>I have a problem: I add all buttons and other outlets manually in the <code>loadView()</code> function. Due to the big amount of buttons I add them in the <code>loadView()</code> function to an array. But apparently the <code>loadView()</code> function is called twice so that I don't have 10 objects in my array but 20. Can anyone explain to me why the function is called twice and what I have to do now?</p>
  
  <p>My Code:</p>
  
  <pre><code>private let myCreator = CreatorClass()
  private var loadGameSlots = [UIButton]()
  
  override func loadView() {
      UIApplication.sharedApplication().statusBarHidden = true
  
      for var i=0;i&lt;5;i++ {
          var stringD:String
          var stringE:String
          if (data.delegate.allFileNames.count == 0) || (data.delegate.allFileNames.count == i+1) {
              stringD = "Nichts gespeichert"
              stringE = "Nothing saved"
          }
          else {
              stringD = data.delegate.allFileNames[i] as String
              stringE = data.delegate.allFileNames[i] as String
          }
          let myButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
          self.myCreator.createButton(myButton, frameForIPhone5: CGRect(x: 45, y: 195, width: 230, height: 25), titleD: stringD, titleE: stringE, bold: false, size: 18.0, titleColor: UIColor.whiteColor(), highlightedColor: UIColor.whiteColor(), shadowColor: nil, offset: 0, backgroundImage: "StartingPageButton.png", backgroundColor: UIColor.clearColor(), action: "loadGameSlotTouched:", target: self, viewToAdd: self.view)
          self.loadGameSlots += [myButton]
          myButton.hidden = true
      }
  }
  </code></pre>
  
  <p>The <code>.createButton</code> method is kind of a custom class:</p>
  
  <pre><code>func createButton(button:UIButton!,var frameForIPhone5:CGRect?,titleD:String?,titleE:String?,bold:Bool?,var size:CGFloat,titleColor:UIColor?,highlightedColor:UIColor?,shadowColor:UIColor?,var offset:CGFloat?,backgroundImage:String?,backgroundColor:UIColor,action:Selector,target:AnyObject?,viewToAdd:UIView?) {
  
      func adjustSizes () {
          let constantY:CGFloat = viewRect.height / 568.0
          let constantX:CGFloat = viewRect.width / 320.0
          frameForIPhone5! = CGRect(x: frameForIPhone5!.origin.x * constantX, y: frameForIPhone5!.origin.y * constantY, width: frameForIPhone5!.width * constantX, height: frameForIPhone5!.height * constantY)
          size *= constantY
          offset! *= constantY
      }
      adjustSizes()
      button.frame = frameForIPhone5!
      if myData.delegate.selectedLanguage == "Deutsch" {
          button.setTitle(titleD!, forState: UIControlState.Normal)
      }
      else {
          button.setTitle(titleE!, forState: UIControlState.Normal)
      }
      if bold! {
          button.titleLabel!.font = UIFont(name: "Futura-CondensedExtraBold", size: size)
      }
      else {
          button.titleLabel!.font = UIFont(name: "Futura-Medium", size: size)
      }
      button.setTitleColor(titleColor?, forState: UIControlState.Normal)
      button.setTitleColor(highlightedColor?, forState: UIControlState.Highlighted)
      button.titleLabel!.shadowColor = shadowColor?
      button.titleLabel!.shadowOffset = CGSize(width: offset!, height: offset!)
      button.setBackgroundImage(UIImage(named: backgroundImage!), forState: UIControlState.Normal)
      button.backgroundColor = backgroundColor
      button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
      viewToAdd?.addSubview(button)
  }
  </code></pre>
  
  <p>It's a lot of code now, but I don't know what you need to answer :)</p>
  
  <p>P.S.: I didn't write anything in the <code>viewDidLoad()</code> function</p>
  
  <p>Output:</p>
  
  <pre><code>(lldb) bt
  * thread #1: tid = 0x66195, 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -&gt; () + 10413 at StartingPageViewController.swift:51, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    * frame #0: 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -&gt; () + 10413 at StartingPageViewController.swift:51
      frame #1: 0x0000000102bec4f2 Airline Star Swift`@objc Airline_Star_Swift.StartingPageViewController.loadView (Airline_Star_Swift.StartingPageViewController)() -&gt; () + 34 at StartingPageViewController.swift:0
      frame #2: 0x0000000103798ef9 UIKit`-[UIViewController loadViewIfRequired] + 75
      frame #3: 0x000000010379938e UIKit`-[UIViewController view] + 27
      frame #4: 0x00000001036c3964 UIKit`-[UIWindow handleStatusBarChangeFromHeight:toHeight:] + 948
      frame #5: 0x00000001036c6308 UIKit`+[UIWindow _noteStatusBarHeightChanged:oldHeight:forAutolayoutRootViewsOnly:] + 235
      frame #6: 0x0000000103676a2e UIKit`__79-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:]_block_invoke + 141
      frame #7: 0x00000001036e5c42 UIKit`+[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 473
      frame #8: 0x00000001036e5ed8 UIKit`+[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 59
      frame #9: 0x0000000103676901 UIKit`-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:] + 490
      frame #10: 0x0000000103676cf2 UIKit`-[UIApplication setStatusBarHidden:withAnimation:] + 126
      frame #11: 0x0000000102be93a8 Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -&gt; () + 152 at StartingPageViewController.swift:18
      frame #12: 0x0000000102bec4f2 Airline Star Swift`@objc Airline_Star_Swift.StartingPageViewController.loadView (Airline_Star_Swift.StartingPageViewController)() -&gt; () + 34 at StartingPageViewController.swift:0
      frame #13: 0x0000000103798ef9 UIKit`-[UIViewController loadViewIfRequired] + 75
      frame #14: 0x000000010379938e UIKit`-[UIViewController view] + 27
      frame #15: 0x00000001036b8db9 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 58
      frame #16: 0x00000001036b9152 UIKit`-[UIWindow _setHidden:forced:] + 276
      frame #17: 0x00000001036c565c UIKit`-[UIWindow makeKeyAndVisible] + 42
      frame #18: 0x0000000103670191 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
      frame #19: 0x0000000103672e5c UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
      frame #20: 0x0000000103671d22 UIKit`-[UIApplication workspaceDidEndTransaction:] + 179
      frame #21: 0x00000001064c82a3 FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke + 16
      frame #22: 0x0000000102d1fabc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
      frame #23: 0x0000000102d15805 CoreFoundation`__CFRunLoopDoBlocks + 341
      frame #24: 0x0000000102d155c5 CoreFoundation`__CFRunLoopRun + 2389
      frame #25: 0x0000000102d14a06 CoreFoundation`CFRunLoopRunSpecific + 470
      frame #26: 0x0000000103671799 UIKit`-[UIApplication _run] + 413
      frame #27: 0x0000000103674550 UIKit`UIApplicationMain + 1282
      frame #28: 0x0000000102be6ede Airline Star Swift`top_level_code + 78 at AppDelegate.swift:13
      frame #29: 0x0000000102be6f1a Airline Star Swift`main + 42 at AppDelegate.swift:0
      frame #30: 0x0000000105107145 libdyld.dylib`start + 1
  (lldb) 
  </code></pre>
  
  <p>And after the second breakpoint:</p>
  
  <pre><code>(lldb) bt
  * thread #1: tid = 0x66195, 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -&gt; () + 10413 at StartingPageViewController.swift:51, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    * frame #0: 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -&gt; () + 10413 at StartingPageViewController.swift:51
      frame #1: 0x0000000102bec4f2 Airline Star Swift`@objc Airline_Star_Swift.StartingPageViewController.loadView (Airline_Star_Swift.StartingPageViewController)() -&gt; () + 34 at StartingPageViewController.swift:0
      frame #2: 0x0000000103798ef9 UIKit`-[UIViewController loadViewIfRequired] + 75
      frame #3: 0x000000010379938e UIKit`-[UIViewController view] + 27
      frame #4: 0x00000001036b8db9 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 58
      frame #5: 0x00000001036b9152 UIKit`-[UIWindow _setHidden:forced:] + 276
      frame #6: 0x00000001036c565c UIKit`-[UIWindow makeKeyAndVisible] + 42
      frame #7: 0x0000000103670191 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
      frame #8: 0x0000000103672e5c UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
      frame #9: 0x0000000103671d22 UIKit`-[UIApplication workspaceDidEndTransaction:] + 179
      frame #10: 0x00000001064c82a3 FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke + 16
      frame #11: 0x0000000102d1fabc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
      frame #12: 0x0000000102d15805 CoreFoundation`__CFRunLoopDoBlocks + 341
      frame #13: 0x0000000102d155c5 CoreFoundation`__CFRunLoopRun + 2389
      frame #14: 0x0000000102d14a06 CoreFoundation`CFRunLoopRunSpecific + 470
      frame #15: 0x0000000103671799 UIKit`-[UIApplication _run] + 413
      frame #16: 0x0000000103674550 UIKit`UIApplicationMain + 1282
      frame #17: 0x0000000102be6ede Airline Star Swift`top_level_code + 78 at AppDelegate.swift:13
      frame #18: 0x0000000102be6f1a Airline Star Swift`main + 42 at AppDelegate.swift:0
      frame #19: 0x0000000105107145 libdyld.dylib`start + 1
  </code></pre>
  
  <p>I'm confused because I did the exactly same thing in Objective-C but it worked perfectly</p>
  
  <p>Thanks in advance! :)</p>