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

时间:2013-03-04 21:25:36

标签: ios objective-c uipageviewcontroller

我有一个带故事板的应用程序。我正在使用多个带有segue的View Controller,可根据所选项目从表格视图中进行选择。我想要一个页面视图控制器,其中页面将是故事板中的一个或多个视图控制器,甚至重复它们。我正在尝试init页面视图控制器,如下所示:     ....     self.dataSource =自我;

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,就我的情况而言是故事板。

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

3 个答案:

答案 0 :(得分:53)

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

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

在故事板中,确保将视图控制器的id设置为vc-identifier;无论你选择什么标识符。

答案 1 :(得分:4)

Swift 3

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

答案 2 :(得分:0)

夫特

如果您想从SELECT ID, UniqueProcutId, [Year], [Month], SUM(Value) OVER(Partition BY YEAR ORDER BY [Month] ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM YourTable 对象初始化。

UIViewController