【问题标题】:Set storyboard ID programmatically for custom UIViewController?以编程方式为自定义 UIViewController 设置情节提要 ID?
【发布时间】:2015-10-11 02:33:59
【问题描述】:

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

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

由于每个自定义UIViewController 以编程方式设置Storyboard 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 需要由代码定义,而不是在可视化编辑器中手动定义。

【问题讨论】:

  • 你在实例化它之后就覆盖了你的变量,如果你的目标是从情节提要中实例化一个 ViewController 然后删除 [[AssetViewer alloc] init]
  • alloc init 有效,但instantiateViewControllerWithIdentifier 因为我还没有设置assetViewer_A 的情节提要。我需要先设置 Storyboard ID,然后才能调用instantiateViewControllerWithIdentifier。注意:我需要以编程方式设置 Storyboard ID,而不是从编辑器中设置。

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


【解决方案1】:

在情节提要中定义一次资产视图控制器,并为其指定情节提要 ID“id_assetViewer”。

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

无需使用不同的 Storyboard ID。

【讨论】:

  • 我需要以编程方式进行。我不想使用可视化编辑器来定义 Storyboard ID。换句话说:两个viewcontroller的Storyboard ID需要通过代码来定义,而不是人为的。
  • 好的,但你还没有解释原因。没有必要让您的示例代码正常工作。
  • 不,我知道没有必要,我只是想这样做。我不喜欢花时间在图形环境中。这是我的偏好。但是,这不仅仅是我的偏好,我想这样做,因为我想通过代码创建不同的自定义 UIViewControllers。
  • 所以现在不要误会我的意思,但你的回答并没有真正回答我的问题。它在标题中说我想以编程方式进行。
猜你喜欢
  • 2015-04-21
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
相关资源
最近更新 更多