Interface Builder: How to load view from nib file

前端 未结 4 1891
日久生厌
日久生厌 2021-02-10 19:20

I have a MyCustomView subclassed from NSView designed in a .xib.

I would like to insert this view into some of my other xib\'s round my application. How should

4条回答
  •  时光说笑
    2021-02-10 19:49

    Use a view controller as it will handle nib loading for you and provide a place to hook up IBOutlet and IBActions in a reusable way.

    In your app delegate or whatever controller create an instance of your view controller. Ask your view controller to load its view. Cast the return type to your view class name. Then keep a reference to your view controller and possibly the view.

    Tell whatever view to add your view as a subview. Add any layout constraints. ( you can build out very generic constraints to add themselves in your view or view controller by overriding viewDidMoveToSuperview or viewDidMoveToWindow when superview or window are not nil. Use the same to remove your constraints. )

    Oddly you remove a view by telling it to remove itself from its superview.

提交回复
热议问题