How to lazy load views programmatically from story

2019-05-30 03:41发布

问题:

In previous versions of Interface Builder, when I want to lazy load views programatically, I would do something like:

self.customView = [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil];

If I understand correctly, this was the proper way to do it. There does not seem to be a way to do this with storyboards, as there is no way to add a view into the storyboard itself(without being part of a view controller).

I have a view controller that manages multiple views. I do not want them all to be loaded on viewDidLoad, so I'd rather not include them in view controller's nib. Is there another way? if not, I could still create nibs and still use loadNibNamed. Can someone advise on whether this is an appropriate strategy, to mix nibs and storyboards? The fact that Apple has disallowed this kind of view loading in storyboards makes me slightly worried.

回答1:

Can someone advise on whether this is an appropriate strategy, to mix nibs and storyboards?

XIBS and Storyboards are not mutually exclusive. There is no reason why you should not use both in the same application. I'm actually using multiple Storyboards and xibs in the same application. Works fine.