Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using View Controller from storyboard programmatically in a Page View Controller

Tags:

I have an app with a storyboard. I am using several View Controller with segues to choose from a table view depending on the item selected. I want to have a page view controller in which the pages will be one or more view controller from the storyboard, even repeating them. I am trying to init the Page View Controller like this: .... self.dataSource=self;

UIViewController *initialViewController =[self viewControllerAtIndex:current]; NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];   [self setViewControllers:viewControllers direction:UIPageViewControllerNavigationOrientationHorizontal  animated:NO completion:nil]; ....  - (UIViewController *)viewControllerAtIndex:(NSUInteger)index { if ((descriptions.count == 0) ||     (index >= descriptions.count)) {     return nil; }  current=index;  // Create a new view controller and pass suitable data. NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];  Description *description=[DBCompany getDescriptionById:language descriptionId:[[descriptions objectAtIndex:index] integerValue]];  UIViewController *viewController=nil; if(description.frame==100){     viewController=[[Company100ViewController alloc] init];     ((Company100ViewController*)viewController).companyId = companyId; } return viewController; }  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { if ((descriptions.count == 0) ||     (current-1 < 0)) {     return nil; }  return [self viewControllerAtIndex:current-1]; }  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { if ((descriptions.count == 0) ||     (current >= descriptions.count)) {     return nil; }  return [self viewControllerAtIndex:current+1]; } 

However the viewcontroller appears in black. I think is because I am adding the UIviewcontroller class but not connecting it to any view, XIB, in my case with the storyboard.

How can I use the different view controller from the storyboard programmatically to use in a page view controller?

like image 873
Laure_f_o Avatar asked Mar 04 '13 21:03

Laure_f_o


People also ask

How do you call a view controller in storyboard programmatically?

NSString * storyboardName = @"MainStoryboard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"]; [self presentViewController:vc animated:YES completion:nil];

How do I embed a view controller in a navigation controller storyboard?

Step 1: Embed root view controller inside a navigation controller. In your storyboard, select the initial view controller in your hierarchy. With this view controller selected, choose the menu item Editor -> Embed In -> Navigation Controller .


2 Answers

If you do viewController=[[Company100ViewController alloc] init] then yes this not a controller that is associated to a storyboard or an XIB. To load the controller with an XIB you have to do the following:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; Company100ViewController * vc = (Company100ViewController *)[sb instantiateViewControllerWithIdentifier:@"vc-identifier"]; 

In the storyboard make sure you set the view controller's id to vc-identifier; whatever identifier you choose.

like image 63
Rudy Avatar answered Sep 20 '22 11:09

Rudy


Swift 3

let sb = UIStoryboard(name: "MainStoryboard", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "vc-identifier") 
like image 36
Ivan Skrobot Avatar answered Sep 21 '22 11:09

Ivan Skrobot