开发者

How to load an external view in a NIB?

开发者 https://www.devze.com 2022-12-25 13:49 出处:网络
I have a ViewController that has its own NIB. I would like to embed that NIB inside of another master NIB. How can I accomplish this in Interface Builder and开发者_运维百科 how do I reference it in co

I have a ViewController that has its own NIB. I would like to embed that NIB inside of another master NIB. How can I accomplish this in Interface Builder and开发者_运维百科 how do I reference it in code?


Sheehan Alam,

If you create a new TabBar Project in Xcode you can see how an external NIB is loaded from an existing object in IB.

Tab Bar Controller http://img.skitch.com/20100329-c2533dqft4q584424926fxfidb.preview.jpg

You can specify a NIB for a viewController using IB via (Command-1)

First View Controller Attributes http://img.skitch.com/20100329-p83bgd8qyieb5h3dg27beinkj3.preview.jpg


Every NIB has an owner, which is usually the controller for the main view in that NIB. You can create an instance of your view controller quite easily in Interface Builder. Just drag out a view controller object and set the NIB property appropriately.

The problem is that you don't have a way of addressing that controller's view from within Interface Builder. You will need to write code to add the embedded view controller's view as a subview of the master view controller's view. How you actually do this is specific to your application, but it should be enough to have an outlet in the master view controller that refers to the embedded view controller.

Then, in the master view controller's viewDidLoad implementation, you add the embedded view controller's view as a subview of the master view controller's view. You'll probably have to adjust the embedded view's frame to fit properly, too.


Alex's approach should work, but if I understand correctly you would rather prefer a way without having to programmatically [self.view addSubview:embeddedView] in the master view controller's -viewDidLoad method.

I wrote up how we embed custom-view Nibs inside other Nibs in a longish blog post. The crux is overriding -awakeAfterUsingCoder: in your custom view, replacing the object loaded from the "master" Nib with the one loaded from the "embedded" Nib.

Note that our custom controls subclass UIView, not UIViewController (see Apple's docs on Custom view controllers: "You should not use multiple custom view controllers to manage different portions of the same view hierarchy.")

0

精彩评论

暂无评论...
验证码 换一张
取 消