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

前端 未结 7 1896
囚心锁ツ
囚心锁ツ 2020-12-01 21:46

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

相关标签:
7条回答
  • 2020-12-01 21:52

    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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 21:53

    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.

    0 讨论(0)
  • 2020-12-01 22:02

    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()
    
    0 讨论(0)
  • 2020-12-01 22:03

    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.

    0 讨论(0)
  • 2020-12-01 22:03

    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

    0 讨论(0)
  • 2020-12-01 22:05

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题