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

Seg*_*gev 26 xcode cocoa-touch objective-c ios

我有一个UIViewControllerRootViewController和A UIViewControllerNewsViewController.我想在NewsViewController里面展示一个UIView(这UIView只是屏幕的一部分)RootViewController.我正在使用StoryBoard,我的应用程序需要支持iOS 5(因此我无法使用IB中的嵌入式segues和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.
}
Run Code Online (Sandbox Code Playgroud)

我还将两个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];
}
Run Code Online (Sandbox Code Playgroud)

Seg*_*gev 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];
}
Run Code Online (Sandbox Code Playgroud)

迅速

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)
}
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*kov 10

创建子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;
}
Run Code Online (Sandbox Code Playgroud)

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

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