AliSoftware/Reusable

Support UIViewController with xib?

codwam opened this issue · 7 comments

Mmmh could be a very simple addition to do indeed. Care to do a Pull Request?

I think it should be as easy as adding something similar to:

protocol NIBBased {
  static var nibName: String { get }
}

extension NIBBased {
  static var nibName: String {
    return String(describing: self), bundle: )
  }
}

extension NIBBased where Self: UIViewController {
  static func instantiate() -> Self {
    return Self.init(nibName: self.nibName, bundle: Bundle(for: self))
  }
}

… + documentation and tests 😉
Would be a great contribution :)

Yep, closed any time.

Has this feature been implemented yet? It doesn't seem like.

Nope, I don't think anyone has found the time to implement that yet indeed 😅

Hi @AliSoftware ,

I believe this issue is resolved in pull request #82

Should be once merged indeed, thanks for the PR 👍 just took a look and commented there 🙂

Just realized that all this functionality is already handled for us by Apple: when you call init on a UIViewController, it forwards it to the designated init(nibName:bundle:) with nil parameters… and when nibName is nil, iOS automatically searches for a XIB of the same name as the class and use it if it finds one!

This is detailed here in Apple's documentation. See also #82 (review)

That means that given a class CustomViewController: UIViewController { … } if you also have a CustomViewController.xib file in your bundle then using let vc = CustomViewController() will automatically use the XIB for loading the VC without yo needing to specify anything.

Hence I'm gonna close this issue since… there's no need for Reusable here, this is behavior provided by iOS for UIViewControllers already 😉