iOS 8.4 - iboutlet not populating in programmatically created view controllers

旧巷老猫 提交于 2019-12-13 05:22:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32833916/ios-8-4-iboutlet-not-populating-in-programmatically-created-view-controllers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!