以编程方式为自定义UIViewController设置故事板ID?

时间:2015-10-11 02:34:00

标签: ios objective-c iphone uiviewcontroller compiler-errors

我创建了一个新的自定义UIViewController

我将使用这个自定义UIViewController中的两个UIPageViewController,以便我可以左右滑动它们。

由于每个自定义Storyboard ID

以编程方式设置UIViewController的性质,我无法解决此问题

正如您在下面的代码中所看到的,我从来没有超过我用注释标记的点,因为我得到以下错误: Storyboard (<UIStoryboard: 0x7f9f404174c0>) doesn't contain a view controller with identifier 'id_AssetViewer_A'

注意:我需要以编程方式设置Storyboard ID不要来自编辑器!

我的代码:

   UIPageViewController *pageController = [self.storyboard instantiateViewControllerWithIdentifier: @"BrowserPageController"];
    pageController.dataSource = self;

    [[pageController view] setFrame:[[self view] bounds]];

    assetViewer_A = [[AssetViewer alloc] init];
    assetViewer_A = [self.storyboard instantiateViewControllerWithIdentifier: @"id_AssetViewer_A"]; //<< error occurs here!
    assetViewer_A.view.tag = 0;

    assetViewer_B = [[AssetViewer alloc] init];
    assetViewer_B = [self.storyboard instantiateViewControllerWithIdentifier: @"id_AssetViewer_B"];
    assetViewer_B.view.tag = 0;


    NSArray *startingViewControllers = [NSArray arrayWithObject: assetViewer_A];
    [pageController setViewControllers: startingViewControllers
                             direction: UIPageViewControllerNavigationDirectionForward
                              animated: NO
                            completion: nil];


    self.browserViewController = pageController;
    [self addChildViewController: self.browserViewController];
    [self.view addSubview: self.browserViewController.view];
    [self.browserViewController didMoveToParentViewController: self];

    if ([pageController.view.subviews.firstObject isKindOfClass:[UIScrollView class]]) {
        browserPageView_as_ScrollView = (UIScrollView *)pageController.view.subviews.firstObject;
        browserPageView_as_ScrollView.delegate = self;
    }

PS: Storyboard ID需要由代码定义,而不是在可视化编辑器中手动完成。

1 个答案:

答案 0 :(得分:0)

在故事板中定义资产视图控制器一次,并为其提供故事板ID&#34; id_assetViewer&#34;。

然后在您实例化资产查看者(A和B)的两个地方的代码中使用此ID。

无需使用不同的Storyboard ID。