Can't Load UIViewController XIB file in Storyboard in Swift
What's happened is that Seed 5 broke the mechanism whereby a view controller can find its xib by name, if the view controller is a Swift class. The reason is that the name of the class, in Swift's mind, is not the same as the name you gave it (and the name you gave the xib file); the name has been "mangled", in particular by prepending the module name (i.e. Swift classes have namespacing).
I offer three workarounds:
Your workaround is a good one (load the .xib file by name explicitly)
Name the .xib file MyModule.TestViewController.xib, where
MyModule
is the name of your bundle (i.e. the name of the project) (this is what Apple advises, but I hate it)Use
@objc(TestViewController)
before the view controller'sclass
declaration to overcome the name mangling which is what's breaking the mechanism (this is the approach I favor)
See my discussion here: https://stackoverflow.com/a/25163757/341994 and my further discussion linked to from there: https://stackoverflow.com/a/25152545/341994
EDIT This bug is fixed in iOS 9 beta 4. If the nib file search fails, iOS 9 now strips the module name off the view controller class name and performs the nib file search a second time.
All IBOutlets become nil after switching to Xcode 6 Beta 5
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.
IBOutlets from xib are not initialised if I run an app on iOS8 Simulator
I was able to fix this issue by adding
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
let name = NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
super.init(nibName: nibNameOrNil ?? name, bundle: nibBundleOrNil)
}
to the view controller class. But why it works this way? Is there another way to fix this issue?
Update:
It is an Swift view controllers feature that is very similar to a bug. Check Can't Load UIViewController XIB file in Storyboard in Swift and Are view controllers with nib files broken in ios 8 beta 5?
It is fixed in iOS 9.4 but if you need iOS 8 and earlier support you still need to use workarounds.
Trying to load a nib programatically receiving myriad errors that weren't encountered in Xcode 6
So I proceeded to hook the File's owner up to the view controller that is responsible for setting up the view
No, you were right the first time. Your problem is that there was a bug having to do with nib names in iOS 8, it was fixed in iOS 9, and you got caught in the net because the bug, ironically, protected you from stumbling across this issue previously.
Do this:
Leave the File's Owner configured as an NSObject.
Do not hook up any outlets from the File's Owner to the view in the nib; if there is such an outlet, delete it.
Load the nib with a
nil
owner, just as you are doing.(Ready for this? This is the key:) Rename the nib. Don't call it TipView.xib. That name was what caused the whole issue you've been trying to work around. Call it, let's say, TipViewNib.xib. Revise your code accordingly, i.e. load the nib with
nibName: "TipViewNib"
.Clear all caches including the simulator version (as I explain here: How to Empty Caches and Clean All Targets Xcode 4) to get rid of the bad nib (the one with the name TipView.xib).
Your problems will then evaporate like morning mist.
Xcode Beta 11: Where is the Host View Controller?
The answer is Yes, you must run Xcode on Catalina to use the Host View Controller. It will not show in the list of Objects on any previous macOSs.
Related Topics
Uiviewcontroller In-Call Status Bar Issue
How to Express Strings in Swift Using Unicode Hexadecimal Values (Utf-16)
Avcapturesession Audio Doesn't Work for Long Videos
Massive Memory Leak in iOS Uiwebview
Alert View Is Showing White Rectangle in iOS7
How to Draw a Circle Path with Color Gradient Stroke
Create PDF with Multiple Pages
iOS Getting Location Updates When App Terminated Without Using Significantchange
How to Copy SQLite Database When Application Is Launched in iOS
How to Create a Development Framework in iOS Including Swift
Find Attributes from Attributed String That User Typed
Uitextfield -Webview No Longer Supported
How Come I Can Cast to Nsmanagedobject But Not to My Entity's Type
How to Tap on a Specific Point Using Xcode Uitests
Fbsdkloginmanager Loginwithpublishpermissions Always Returns Iscancelled=Yes
Wkwebview Function for Detecting If the Url Has Changed