ios 使用 StoryBoard 在容器视图中加载 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17852695/
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-08-31 00:29:34  来源:igfitidea点击:

Load UIViewController inside a Container View using StoryBoard

iosobjective-cxcodecocoa-touch

提问by Segev

I have a UIViewControllercalled RootViewControllerand A UIViewControllercalled NewsViewController. I want to show NewsViewControllerinside a UIView(the UIViewis just a part of the screen) I created inside RootViewController. I'm using StoryBoard and my app needs to support iOS 5 (so I can't use embedded segues aka Containers from the IB)

我有一个UIViewController电话RootViewController和一个UIViewController电话NewsViewController。我想在NewsViewController里面显示UIView(这UIView只是屏幕的一部分)我在里面创建的RootViewController。我正在使用 StoryBoard 并且我的应用程序需要支持 iOS 5(所以我不能使用来自 IB 的嵌入式 segues aka Containers)

RootViewController code:

RootViewController 代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NewsViewController *news = [[NewsViewController alloc]init];
    news.view.frame = self.newsSection.bounds;
    [self.newsSection addSubview:news.view];
    [self addChildViewController:news];
    // Do any additional setup after loading the view.
}

I also connected both UIViewControllers with a segue. The UIView newsSection will stay empty. What Am I doing wrong?

我还使用 segue 连接了两个 UIViewControllers。UIView newsSection 将保持为空。我究竟做错了什么?

Edit:

编辑:

This works for me, is that the right approach?

这对我有用,这是正确的方法吗?

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
    news.view.frame = self.newsSection.bounds;
    [self.newsSection addSubview:news.view];
    [self addChildViewController:news];
    [news didMoveToParentViewController:self];
}

回答by Segev

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
    news.view.frame = self.newsSection.bounds;
    [self.newsSection addSubview:news.view];
    [self addChildViewController:news];
    [news didMoveToParentViewController:self];
}

Swift

迅速

override func viewDidLoad() {
    super.viewDidLoad()
    let news = self.storyboard?.instantiateViewControllerWithIdentifier("NewsViewControllerID") as! NewsViewController
    news.view.frame = newsSection.bounds
    newsSection.addSubview(news.view)
    addChildViewController(news)
    news.didMoveToParentViewController(self)
}

Swift >= 3:

斯威夫特> = 3:

override func viewDidLoad() {
    super.viewDidLoad()
    let news = self.storyboard?.instantiateViewController(withIdentifier: "NewsViewControllerID") as! NewsViewController
    news.view.frame = newsSection.bounds
    newsSection.addSubview(news.view)
    addChildViewController(news)
    news.didMove(toParentViewController: self)
}

回答by Alexey Kozhevnikov

Create child, when creating root VC:

创建子级,在创建根 VC 时:

-(void)awakeFromNib
{
  [super awakeFromNib];
  // Create news vc from storyboard
  UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
  NewsViewController *news = [storyboard instantiateViewControllerWithIdentifier:@"News VC ID, you should set it in IB"];
  // Add it as a child to root
  [self addChildViewController:news];
  [news didMoveToParentViewController:self];
  // Save it for future use
  self.news = news;
}

Add child's view when root's view is created:

创建根视图时添加子视图:

-(void)viewDidLoad
{
  [super viewDidLoad];
  self.news.view.frame = self.newsSection.bounds;
  [self.newsSection addSubview:self.news.view];
}

回答by Lithu T.V

Try this:

尝试这个:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" 
                                                     bundle:nil];
NewsViewController *rootViewController = 
 [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];

回答by Vassily

        otherController!.view.frame = container.bounds;
        self.addChildViewController(otherController!);
        container.addSubview(otherController!.view);
        container.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: .allZeros, metrics: nil, views: ["view": otherController!.view]))
        container.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: .allZeros, metrics: nil, views: ["view": otherController!.view]))
        otherController!.didMoveToParentViewController(self);

Don't forget to specify constraint especially if you want to perform animations

不要忘记指定约束,特别是如果你想执行动画

回答by ProgrammingNinja

Here is the approach the way it worked for me.

这是它对我有用的方法。

I have 3 buttons at the top Purchase,Sale, Rental. now at tapping at any particular button i am loading corresponding UIViewController and i am using storyboard.

我在购买、销售、租赁顶部有 3 个按钮。现在在点击任何特定按钮时,我正在加载相应的 UIViewController 并且我正在使用故事板。

I specified Storyboard Idfor every UIViewController.

Storyboard Id为每个UIViewController.

I added two UIViewto my Main ViewControllerwhich have these 3 buttons(Purchase, Sale ,Rental).

我添加了两个UIViewMain ViewController其中有这 3 个按钮(购买、销售、租赁)。

one view i am using for the above three buttons and other i am using for loading a UIViewController.

一个视图用于上述三个按钮,另一个视图用于加载 UIViewController。

Now at tapping a specific button i am and in tapping a specific button i am doing following code.

现在在点击特定按钮时我正在点击特定按钮,我正在执行以下代码。

- (IBAction)sellingClicked:(id)sender
{        
    UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];

    TestViewController *tvc=[storyboard instantiateViewControllerWithIdentifier:@"test"];

    [self.viewContainer addSubview:tvc.view];
    [self addChildViewController:tvc];       
}

and its working perfectly for me further you can add scrolling View i you want to scroll there.

并且它对我来说非常有用,您可以添加滚动视图,我想在那里滚动。

回答by stosha

You should move your code to - (void) viewDidAppear: (BOOL) animated like this

你应该将你的代码移动到 - (void) viewDidAppear: (BOOL) 像这样动画

- (void) viewDidAppear: (BOOL) animated
{
    [super viewDidAppear: animated];

    NewsViewController *news = [[NewsViewController alloc]init];
    news.view.frame = self.newsSection.bounds;
    [self.newsSection addSubview:news.view];
    [self addChildViewController:news];
    // Do any additional setup after loading the view.
}

because frame and bounds in viewDidLoad is {0,0} {0,0}

因为 viewDidLoad 中的 frame 和 bounds 是 {0,0} {0,0}