Subclassing a subclassed UIViewController that has a xib

♀尐吖头ヾ 提交于 2019-12-01 04:01:53

If you only have an xib for the parent class (but none of the subclasses), you can just do this in your subclass init:

- (instancetype) init {
    if (self = [super initWithNibName:@"ParentViewController" bundle:nil]) {
      // init stuff for subclass
    }
    return self;
 }

Here's an example project:

https://github.com/annabd351/SubClassFromParentNib

If someone is looking for a Swift solution, you can use Convenience initializers on the subclass and initialise it using the super class xib.

In your subclass use the convenience initializer as:

class ChildController: BaseViewController {

    convenience init() {
        self.init(nibName: "BaseViewControllerXib", bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

And then when creating the child class object just use.

let childViewController = ChildController()
navigationController.pushViewController(childViewController, animated: true)

This way you can use the xib from the parent viewController.

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