Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load UIViewController inside a Container View using StoryBoard

I have a UIViewController called RootViewController and A UIViewController called NewsViewController. I want to show NewsViewController inside a UIView (the UIView is 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)

RootViewController code:

- (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?

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]; } 
like image 517
Segev Avatar asked Jul 25 '13 08:07

Segev


People also ask

How do I connect two view controllers in storyboard?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.


1 Answers

- (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:

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) } 
like image 191
Segev Avatar answered Oct 27 '22 00:10

Segev