12

When running iOS 8, the IBOutlets of my nib based view controllers are nil, but when running iOS 9, they work fine. Any ideas why this might be happening?

My app supports iOS 8+, and I'm running Xcode 7.2.

Vatsal Manot
  • 17,695
  • 9
  • 44
  • 80
jjramos
  • 1,874
  • 14
  • 22

1 Answers1

11

The problem was in the way my UIViewController was instantiated. Changing let myVC = MyViewController()

for let myVC = MyViewController(nibName: "MyViewController", bundle: nil) fixed the problem.

jjramos
  • 1,874
  • 14
  • 22
  • 4
    Instead of passing `nil` for the bundle, you can use `NSBundle(forClass: self.dynamicType)` – orkoden Jan 04 '16 at 11:31
  • Thanks for the suggestion. Would it be more accurate `NSBundle(forClass: MyViewController.self)`? – jjramos Jan 04 '16 at 11:53
  • 1
    And even better: `let myVC = MyViewController(nibName: String(MyViewController), bundle: NSBundle(forClass: MyViewController.self))` – jjramos Jul 05 '16 at 10:47
  • @jjramos: Has this stopped working with Xcode 7.3? In my case I get this error: `Type of expression is ambiguous without more context` while instantiating the View Controller as suggested above. I cannot work around this, despite adding explicit types, casts etc... – Nick Kanellopoulos Aug 12 '16 at 16:29
  • EDIT: I had added a custom init method. Adding an override for `init(nibName:, bundle: )` (and calling super in it) did it. Thanks! – Nick Kanellopoulos Aug 12 '16 at 16:38
  • In Swift 4: you can use `MyViewController(nibName: String(describing: MyViewController.self), bundle: Bundle(for: type(of: self)))` – Amjad Husseini Nov 15 '17 at 10:02