Using View Controller from storyboard programmatically in a Page View Controller

可紊 提交于 2019-11-29 11:01:53

问题


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?


回答1:


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.




回答2:


Swift 3

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



回答3:


Swift

In case that you want to init from UIViewController object.

guard let vc = storyboard?.instantiateViewController(withIdentifier: "nameVC") else {
    return
}

// add to current view
view.addSubview(vc.view)


来源:https://stackoverflow.com/questions/15211441/using-view-controller-from-storyboard-programmatically-in-a-page-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!