在页面视图控制器中以编程方式使用故事板中的View Controller [英] Using View Controller from storyboard programmatically in a Page View Controller
问题描述
我有一个带故事板的应用程序。我正在使用多个带有segue的View Controller,可根据所选项目从表格视图中进行选择。我想要一个页面视图控制器,其中页面将是故事板中的一个或多个视图控制器,甚至重复它们。我正在尝试 init
页面视图控制器,如下所示:
....
self.dataSource = self;
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];
}
然而 viewcontroller
出现在黑色中。我想是因为我正在添加 UIviewcontroller
类,但在我的情况下没有将它连接到任何视图,XIB和故事板。
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?
推荐答案
如果你执行viewController = [[Company100ViewController alloc] init],那么这不是一个与故事板或XIB相关联的控制器。要使用XIB加载控制器,您必须执行以下操作:
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"];
在故事板中确保将视图控制器的id设置为vc-identifier;无论你选择什么标识符。
In the storyboard make sure you set the view controller's id to vc-identifier; whatever identifier you choose.
这篇关于在页面视图控制器中以编程方式使用故事板中的View Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!