Reputation: 2530
I created a test project in ios 8 beta 4 which as a main view controller and a second view controller created as a UIViewController subclass with a xib file.
I put a button on the main controller to present the second controller:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func testVCBtnTapped() {
let vc = TestVC()
presentViewController(vc, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I run the app and pressing the button presents the second controller - all is well
Moving to xcode beta 5, I run the app and when I press the button the screen goes black.
Since I know they messed with the init code, I tried putting in overrides to see it that would fix it:
class TestVC: UIViewController {
override init() {
super.init()
}
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
Same problem. Changing the required and overrides in to all possible combinations accepted by xcode has no effect.
If I use the storyboard to create another controller and segue to it all is well.
Any ideas?
EDIT - New info
Apparently a swift beta 5 problem
Upvotes: 12
Views: 6764
Reputation: 4092
I had a very similar problem on Xcode 10.1 and iOS 12.
This code worked:
class ExampleViewController: UIViewController {
init() {
super.init(nibName: "ExampleViewController", bundle: nil)
}
}
But passing nil as nibName
made it not load the XIB.
When specifying the class name explicitly via @objc("ExampleViewController") this fixed it.
The problem was caused by the module name starting with the letters 'UI' (the project was called UIKitExample). When I renamed the target to something else, this fixed the problem.
Upvotes: 0
Reputation: 21
I've had this problem recently, and my solution is to override the nibName method in baseviewController. My solution is as follows:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(nibName: nil, bundle: Bundle.main)
}
override var nibName: String? {
get {
if #available(iOS 9, *) {
return super.nibName
} else {
let classString = String(describing: type(of: self))
guard Bundle.main.path(forResource: classString, ofType: "nib") != nil else {
return nil
}
return classString
}
}
}
Upvotes: 2
Reputation: 65
Swift3:
extension UIViewController {
static func instanceWithDefaultNib() -> Self {
let className = NSStringFromClass(self).components(separatedBy: ".").last
return self.init(nibName: className, bundle: nil)
}
}
Upvotes: 0
Reputation: 472
Here's code, based on Francesco answer. It contains check if nib file exits, so app will not crashed when you load UIViewController which doesn't have associated xib (you can reproduce it on iOS8)
override var nibName: String? {
get {
let classString = String(describing: type(of: self))
guard nibBundle?.path(forResource: classString, ofType: "nib") != nil else {
return nil
}
return classString
}
}
override var nibBundle: Bundle? {
get {
return Bundle.main
}
}
Upvotes: 0
Reputation: 1119
This trick works for me. If you have a base view controller class you can override the nibName property returning the class name (equals to the xib file name).
class BaseViewController: UIViewController {
override var nibName: String? {
get {
guard #available(iOS 9, *) else {
let nib = String(self.classForCoder)
return nib
}
return super.nibName
}
}
}
All view controllers that inherit from this base class could load their xib. e.g. MyViewController: BaseViewController could load MyViewController.xib
Upvotes: 3
Reputation: 1601
in case if You need support of iOS8. You can use extension on UIViewController
extension UIViewController {
static func instanceWithDefaultNib() -> Self {
let className = NSStringFromClass(self as! AnyClass).componentsSeparatedByString(".").last
let bundle = NSBundle(forClass: self as! AnyClass)
return self.init(nibName: className, bundle: bundle)
}
}
and then, just create the instance this way
let vc = TestVC.instanceWithDefaultNib()
Upvotes: 4
Reputation: 535954
I can't tell whether it's a bug or not, but it is definitely a change. Of course they might change it back... In any case, the rule in seed 5 is:
The view controller will not automatically find its .xib just because it has the same name. You have to supply the name explicitly.
So in your case any attempt to initialize this view controller must ultimately call nibName:bundle:
with an explicit nib name ("TestVC"
), I assume.
If you want to be able to initialize by calling
let vc = TestVC()
as you are doing in your presenting view controller, then simply override init()
in the presented view controller to call super.init(nibName:"TestVC", bundle:nil)
and that's all you need (except that I presume you will also need the init(coder:)
stopper discussed here).
EDIT You are absolutely right that this is a Swift-only problem. Well spotted. In Objective-C, initializing with init
(or new
) still works fine; the view controller finds its eponymous .xib file correctly.
ANOTHER EDIT The determining factor is not whether Objective-C or Swift calls init
. It is whether the view controller itself is written in Objective-C or Swift.
FINAL EDIT The workaround is to declare your Swift view controller like this:
@objc(ViewController) ViewController : UIViewController { // ...
The name in parentheses gets rid of the name mangling which is causing the problem. It is likely that in the next release this will be fixed and you can take the @objc
away again.
ANOTHER FINAL EDIT Bad news: the bug report I filed on this came back "works as intended". They point out that all I have to do is name the .xib file after the surrounding module, e.g. if my app is called NibFinder, then if I name my .xib file NibFinder.ViewController.xib, it will be found automatically when instantiating ViewController()
.
That is true enough, but in my view it merely restates the bug; the Swift lookup procedure is prepending the module name. So Apple is saying I should knuckle under and prepend the same module name to my .xib file, whereas I am saying that Apple should knuckle under and strip the module name off as it performs the search.
EDIT THAT IS TRULY TRULY FINAL This bug is fixed in iOS 9 beta 4 and all these workarounds become unnecessary.
Upvotes: 25