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]; }
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.
- (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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With