xcode 将 UIViewController xib 加载到故事板中的 ContainerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14054632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:24:27  来源:igfitidea点击:

Loading a UIViewController xib to ContainerView in storyboard

iphoneobjective-ciosxcodeuistoryboard

提问by Lirik

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.

我有一个“容器”viewController,它使用故事板保存一些容器视图。每个容器视图都有一个指向嵌入式视图控制器视图的链接。

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.

因为我在做一个大项目,所以故事板文件很大并且包含很多“子”视图布局。当几个人需要同时处理该文件时,处理该文件将是一个问题。这对我不利。我想知道是否有办法将视图控制器的 xib 文件加载到每个容器中,并且仍然使用故事板。

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.

意思是,为每个视图控制器创建 .xib 文件而不是将它们保存在故事板本身中,并将它们链接到故事板中的容器视图。

thanks,

谢谢,

回答by Nuthatch

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

可以将视图控制器布局移动到单独的 xib 中。正如您所说,这是跨故事板共享布局的一种非常方便的方式。

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 (e.g. FooViewController.xib so it can be found when FooViewController.m is loaded.)

在故事板中设置容器视图时,请确保删除嵌入式 ViewController 中提供的视图。将 ViewController 的自定义类设置为您的类名。命名 xib 以匹配类(例如 FooViewController.xib,以便在加载 FooViewController.m 时可以找到它。)

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

如果您不删除故事板视图控制器布局中的视图,您将看到该空视图。您viewDidLoad在您的视图控制器的方法将不会被调用,因为该视图是默认的故事板视图。

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

我在 iOS 7 之前的项目中使用 Xcode 6.1 for iOS 8。

回答by Rob

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 Controllersin 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 (e.g. 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 parentStoryboardproperty 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 UIStoryboardproperty can be useful.

  1. 您需要做的就是放入我们在使用 NIB 时使用的那种代码。对于容器,这意味着典型的容器方法。如果您还没有通过代码进行控制,请参阅View Controller Programming Guide中的创建自定义容器视图控制器最重要的是,当您过渡到非故事板场景(或添加非故事板子视图控制器)时,只需像在基于 NIB 的环境中通常使用的那样对其进行编码。您不能在故事板中表示这个基于 NIB 的场景。但是你只是像往常一样使用 NIB 来获得控制器:

    SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    
  2. 您显然失去了使用故事板的许多好处(例如,根据父场景中的容器视图调整子场景的大小),但您的情况并不比在 NIB 环境中更糟。但是,在回答您是否可以“将它们链接到故事板中的容器视图”的问题时,您不能在故事板本身中表示这种关系,而是以编程方式将它们链接起来。

  3. 如果您的独立团队在单场景环境中工作,您可以使用这种 NIB 方法。但是,您还应该考虑是否只拥有多个故事板,每个逻辑团队一个。在故事板之间转换时,您仍然需要使用代码,就像您在这种 NIB 方法中所做的那样,但是如果您的团队之一有多个场景要处理,他们可以在他们的项目部分中享受故事板的好处。当您想进入下一个故事板中的第一个场景时,您可以:

    UIStoryboard *secondaryStoryboard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
    SecondViewController *controller = [secondaryStoryboard instantiateInitialViewController];
    
  4. 如果您的孩子需要转换到故事板上的新场景,我发现将我自己的parentStoryboard属性添加到我的子控制器很有用,如果您需要执行类似instantiateViewControllerWithIdentifier. 显然,如果您没有过渡到新的控制器,您可能不需要这样做,但如果是,拥有一个UIStoryboard属性会很有用。