以编程方式为自定义UIViewController设置storyboard ID



我已经创建了一个新类,它是一个自定义的UIViewController

我将使用两个自定义UIViewController和一个UIPageViewController,以便我可以在它们之间向左和向右滑动。

由于以编程方式设置每个自定义UIViewControllerStoryboard ID的性质,我有问题使其工作

正如你在下面的代码中看到的,我从来没有越过我用注释标记的点,因为我得到了以下错误: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需要在代码中定义,而不是在可视化编辑器中手工定义

在storyboard中定义一次资产视图控制器,并给它一个storyboard ID为"id_assetViewer"。

然后在你的代码中实例化你的资产查看器(A和B)的地方使用这个ID。

没有必要使用不同的故事板id。

最新更新