All IBOutlets become nil after switching to Xcode 6 Beta 5

旧时模样 提交于 2019-12-09 07:00:02

问题


For instance, there is a property in a view controller

@IBOutlet weak var nameLabel: UILabel!

This property is nil inside viewWillAppear and viewDidLoad, so the app crashes at runtime.

It was working fine in Xcode 6 Beta 4. After I switched to Beta 5, it complained about the controller class does not implement its superclass's required members. So I added

required init(coder aDecoder: NSCoder!) {
  super.init(coder: aDecoder)
}

And that compiler error disappeared. However, the app crashes for unexpectedly found nil while unwrapping an Optional value because that nameLabel property is nil when I try to set its text.

I read through the release notes and could not figure out how to fix this issue.


回答1:


I was having the same issue in Beta5. It appears to be a problem where

init(nibName: nil, bundle: nil) 

is not mapping nil to the default nibName. When I changed to an explicit nibName then it worked. Specifically in my case, using the new ?? operator:

override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
   // beta5 workaround: replace nil with explicit name of xib file
   let nib = nibNameOrNil ?? "MyViewController"

   super.init(nibName: nib, bundle: nibBundleOrNil)

   // local initialization here
}

caused it to magically work again.




回答2:


It's a temporary bug. The workaround turns out to be: Declare your view controller in such a way as to override name mangling, like this:

@objc(ViewController) ViewController : UIViewController { // or whatever its name is

See also: Are view controllers with nib files broken in ios 8 beta 5?

EDIT This bug is fixed in iOS 9 beta 4.



来源:https://stackoverflow.com/questions/25150992/all-iboutlets-become-nil-after-switching-to-xcode-6-beta-5

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