26

我有一个UIViewControllerRootViewController和一个UIViewControllerNewsViewController。我想在我创建的内部显示NewsViewControllerUIViewUIView只是屏幕的一部分)RootViewController。我正在使用 StoryBoard,我的应用程序需要支持 iOS 5(所以我不能使用来自 IB 的嵌入式 segues aka Containers)

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.
}

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

编辑:

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

- (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];
}
4

6 回答 6

44
- (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];
}

迅速

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)
}

斯威夫特 >= 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)
}
于 2014-03-25T11:44:20.983 回答
10

在创建根 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;
}

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

-(void)viewDidLoad
{
  [super viewDidLoad];
  self.news.view.frame = self.newsSection.bounds;
  [self.newsSection addSubview:self.news.view];
}
于 2013-07-25T08:48:08.913 回答
1

尝试这个:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" 
                                                     bundle:nil];
NewsViewController *rootViewController = 
 [storyboard instantiateViewControllerWithIdentifier:@"NewsViewControllerID"];
于 2013-07-25T08:48:39.777 回答
1

这是对我有用的方法。

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

Storyboard Id为每个UIViewController.

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

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

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

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

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

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

它对我来说非常完美,您可以添加滚动视图,我想在那里滚动。

于 2013-12-08T18:29:15.200 回答
1
        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);

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

于 2015-09-10T15:22:08.573 回答
0

您应该将代码移动到 - (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.
}

因为 viewDidLoad 中的框架和边界是 {0,0} {0,0}

于 2013-07-25T09:22:43.740 回答