简体   繁体   English

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

[英]Load UIViewController inside a Container View using StoryBoard

I have a UIViewController called RootViewController and A UIViewController called NewsViewController . 我有一个UIViewController称为RootViewController和A UIViewController称为NewsViewController I want to show NewsViewController inside a UIView (the UIView is just a part of the screen) I created inside RootViewController . 我想在UIView显示NewsViewControllerUIView只是屏幕的一部分)我在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) 我正在使用StoryBoard,我的应用程序需要支持iOS 5(因此我无法使用IB中的嵌入式segues和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. 我还将两个UIViewControllers与segue连接起来。 The UIView newsSection will stay empty. UIView newsSection将保持空白。 What Am I doing wrong? 我究竟做错了什么?

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];
}
- (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: Swift> = 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)
}

Create child, when creating root VC: 创建子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];
}

Try this: 尝试这个:

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

Here is the approach the way it worked for me. 这是它对我有用的方法。

I have 3 buttons at the top Purchase,Sale, Rental. 我在顶部购买,销售,租赁有3个按钮。 now at tapping at any particular button i am loading corresponding UIViewController and i am using storyboard. 现在在点击任何特定按钮我正在加载相应的UIViewController,我正在使用故事板。

I specified Storyboard Id for every UIViewController . 我为每个UIViewController指定了Storyboard Id

I added two UIView to my Main ViewController which have these 3 buttons(Purchase, Sale ,Rental). 我在我的Main ViewController添加了两个UIView ,它们有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. 并且它对我的完美工作进一步你可以添加滚动视图我想要在那里滚动。

        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 如果要执行动画,请不要忘记指定约束

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中的边框和边界是{0,0} {0,0}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从情节提要UIViewController将视图加载到外部显示 - Load view from storyboard UIViewController to external display 在容器视图(Storyboard)中推入透明背景UIViewController时为灰色背景 - Grey color background on pushing transparent background UIViewController inside a Container View (Storyboard) 确定UIViewController是否在Container View中 - Determine if UIViewController is inside Container View 在iOS中,使用storyboard,如何在容器视图中设置视图控制器? - In iOS, using storyboard, how to setup a view controller inside a container view? 从Storyboard嵌入到容器视图中的UINavigationController不会推送UIViewController - UINavigationController embedded in container view from Storyboard won't push UIViewController 从故事板内的UIViewController加载UIView不起作用 - Load an UIView from UIViewController inside storyboard doesn't work 我可以使用情节提要板将UINavigationController的视图嵌入UIViewController中吗? - Can I use storyboard to embed UINavigationController's view inside a UIViewController? 使用情节提要的容器视图,交互和视图问题 - Container view using Storyboard, interaction and view issues 使用Storyboard本地化UIViewController - Localizing UIViewController using Storyboard 使用 XIB UIViewController 和 Storyboard - Using XIB UIViewController with Storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM