0

When loading a UIViewController programmatically in iOS 8.4 I am consistently seeing the IBOutlets registering as nil. This is causing crashes on any iOS 8.4 device. The exact same code runs smoothly on iOS 9 and 9.0.1. For reference, this is a snippet of the view controller

class B8AVPermissionsViewController: B8BaseViewController {

    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var cameraButton: UIButton!
    @IBOutlet weak var microphoneButton: UIButton!

    var delegate: B8PermissionRequestingDelegate? = nil;

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated);
        NSLog("cameraButton: \(cameraButton)")
    }

That prints out cameraButton: nil

The code that creates this looks like:

self.permissionViewController = B8AVPermissionsViewController()
self.permissionViewController!.delegate = self
dispatch_async(dispatch_get_main_queue(), {
    self.presentViewController(self.permissionViewController!, animated: true, completion: nil)
})

What am I doing wrong?

Tom Elliott
  • 1,908
  • 1
  • 19
  • 39
Ned Rockson
  • 1,095
  • 7
  • 16

1 Answers1

5

The problem is that there's bug in iOS 8. Saying B8AVPermissionsViewController() in iOS 8 does not automatically load the associated nib. You need to work around this; for example, you could call init(nibName:bundle:) explicitly and tell it where the nib is.

The bug is fixed in iOS 9, which is why you're not seeing the same problem there.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And please see this answer of mine for the gruesome history of this bug and a full list of workarounds: http://stackoverflow.com/a/25152545/341994 – matt Sep 29 '15 at 00:53
  • I just stumbled across this solution while debugging. Suddenly things started working. Came here and it turns out this is something that has been around for ... awhile! What a farce!! – Ned Rockson Sep 29 '15 at 01:00
  • great answer, saved my hours – Shannon Chou Nov 27 '15 at 04:35