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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15211441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:33:51  来源:igfitidea点击:

Using View Controller from storyboard programmatically in a Page View Controller

iosobjective-cuipageviewcontroller

提问by Laure_f_o

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 initthe Page View Controller like this: .... self.dataSource=self;

我有一个带有故事板的应用程序。我正在使用几个带有 segue 的视图控制器,根据所选项目从表视图中进行选择。我想要一个页面视图控制器,其中页面将是故事板中的一个或多个视图控制器,甚至重复它们。我正在尝试这样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];
}

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

但是viewcontroller显示为黑色。我认为是因为我添加了UIviewcontroller类,但没有将它连接到任何视图 XIB,就我而言是故事板。

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

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

回答by Rudy

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:

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

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.

在故事板中,确保将视图控制器的 id 设置为 vc-identifier;您选择的任何标识符。

回答by Ivan Skrobot

Swift 3

斯威夫特 3

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

回答by dimpiax

Swift

迅速

In case that you want to init from UIViewControllerobject.

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

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

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