简体   繁体   中英

Loading a UIViewController xib to ContainerView in storyboard

I have a "container" viewController, that hold a few container views using storyboard. each of that container view has a link to an embedded viewcontroller's view.

Since im working on a big project, it turns out that the storyboard file is massive and contains a lot of "child" viecontrollers view layout. it will be a problem working on that file when a few people needs to be working simultaneously on it. and thats not good for me. i would like to know if theres a way to load into each container view a viewcontroller's xib file, and still be using storyboard.

Meaning, creating .xib files for each viewcontroller instead of holding them in the storyboard itself, and link them to the container views in the storyboard.

thanks,

You can move your view controller layout into a separate xib. As you say, this is a very convenient way to share layouts across storyboards.

When setting up a container view in your storyboard, make sure you delete the provided view in the embedded ViewController . Set the custom class for the ViewController to your class name. Name the xib to match the class (eg FooViewController.xib so it can be found when FooViewController.m is loaded.)

If you don't delete the View in the storyboard view controller layout, you'll see that empty view instead. Your viewDidLoad method in your view controller won't be called, because the View is the default Storyboard View.

I'm using Xcode 6.1 for iOS 8 on a pre-iOS 7 project.

Yes, you can. A few observations:

  1. All you need to do is to put in the sort of code that we used to use when using NIBs. In the case of containers, that means the typical container methods . If you've not done containment via code, see Creating Custom Container View Controllers in the View Controller Programming Guide. Bottom line, as you transition to a non-storyboard scene (or add a non-storyboard child view controller), just code it like you always used to in the NIB-based environment. You cannot represent this NIB-based scene in the storyboard. But you just get the controller like you always do with NIBs:

     SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil]; 
  2. You obviously lose many of the benefits of using storyboards (eg sizing the child scene on the basis of the container view in the parent scene), but you're no worse off than you were in a NIB environment. But in answer to your question as to whether you can "link them to the container views in the storyboard", you cannot represent this relationship in the storyboard itself, but rather you link them up programmatically.

  3. If your separate teams are working in single-scene environments, you can use this NIB approach. You should also contemplate, though, just having multiple storyboards, one for each logical team. You'll still have to resort to code as you transition between storyboards, just like you do in this NIB approach, but if one of your teams has multiple scenes to deal with, they can enjoy storyboard benefits within their portion of the project. When you want to get to the first scene in the next storyboard, you can:

     UIStoryboard *secondaryStoryboard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil]; SecondViewController *controller = [secondaryStoryboard instantiateInitialViewController]; 
  4. If your child needs to transition to a new scene on your storyboard, I find it useful to add my own parentStoryboard property to my child controller, which it can then useful if you need to do something like instantiateViewControllerWithIdentifier . Obviously, if you're not transitioning to new controllers, you may not need to do that, but if you are, having a UIStoryboard property can be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM