在页面视图控制器中以编程方式使用Storyboard中的View Controller

我有一个故事板的应用程序。 我使用几个视图控制器与segues从表视图中select取决于select。 我想要一个页面视图控制器,其中的页面将是故事板中的一个或多个视图控制器,甚至重复它们。 我试图像这样init页面视图控制器:…. 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]; } 

然而, viewcontroller显示为黑色。 我认为是因为我添加UIviewcontroller类,但没有连接到任何视图,XIB,在我的情况下,情节UIviewcontroller

如何以编程方式使用故事板中的不同视图控制器在页面视图控制器中使用?

如果你做viewController = [[Company100ViewController alloc] init],那么是的,这不是一个控制器,故事板或XIB关联。 要使用XIB加载控制器,您必须执行以下操作:

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

在故事板中,确保将视图控制器的ID设置为vc-identifier; 无论您select何种标识符

Swift 3

 let sb = UIStoryboard(name: "MainStoryboard", bundle: nil) let vc = sb.instantiateViewController(withIdentifier: "vc-identifier") 

迅速

如果你想从UIViewController对象初始化。

 guard let vc = storyboard?.instantiateViewController(withIdentifier: "nameVC") else { return } // add to current view view.addSubview(vc.view)