在故事板上推送视图以进行页面控制

发布于 2024-12-23 08:15:09 字数 1598 浏览 2 评论 0原文

我正在尝试实现一个页面控件来显示一些带有以下故事板的页面: 在此处输入图像描述

如您所见,我有一个带有滚动视图(ViewController3)和另一个视图( PageViewController) 代表我想要推送到滚动视图上的那个。

在 ViewController3 的 viewDidLoad 上,我调用以下方法:

- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
    return;
if (page >= NUMBER_OF_PAGES)
    return;

PageViewController *currentViewController = [viewControllers objectAtIndex:page];
if ((NSNull *)currentViewController == [NSNull null])
{
    currentViewController = [[PageViewController alloc] init];
    currentViewController.pageNumber = [NSString stringWithFormat:@"%d", page];
    [viewControllers replaceObjectAtIndex:page withObject:currentViewController];
}

// add the controller's view to the scroll view
if (currentViewController.view.superview == nil)
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    currentViewController.view.frame = frame;
    currentViewController.view.backgroundColor = [UIColor yellowColor];
    [self.scrollView addSubview:currentViewController.view];

    self.scrollView.backgroundColor = [UIColor redColor];

    NSDictionary *numberItem = [contentList objectAtIndex:page];
    currentViewController.numberImage.image = [UIImage imageNamed:[numberItem valueForKey:IMAGE_KEY]];
    currentViewController.numberTitle.text = [numberItem valueForKey:NAME_KEY];
}
}

如果我运行该项目,则显示黄色背景的视图,没有标签,也没有图像视图。

如您所见,我以编程方式设置了背景颜色。根据我的理解,这意味着新视图已正确添加到 ViewController3,但它未链接到情节提要上配置的视图。 您对如何解决这个问题有什么建议吗?

提前致谢, 亚萨

I'm trying to implement a Page Control to show some pages with the following storyboard:
enter image description here

As you can see, I've the main view with a scroll view (ViewController3) and another view (PageViewController) that represents the one that I want to push on my scroll view.

On viewDidLoad of ViewController3 I call the following method:

- (void)loadScrollViewWithPage:(int)page
{
if (page < 0)
    return;
if (page >= NUMBER_OF_PAGES)
    return;

PageViewController *currentViewController = [viewControllers objectAtIndex:page];
if ((NSNull *)currentViewController == [NSNull null])
{
    currentViewController = [[PageViewController alloc] init];
    currentViewController.pageNumber = [NSString stringWithFormat:@"%d", page];
    [viewControllers replaceObjectAtIndex:page withObject:currentViewController];
}

// add the controller's view to the scroll view
if (currentViewController.view.superview == nil)
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    currentViewController.view.frame = frame;
    currentViewController.view.backgroundColor = [UIColor yellowColor];
    [self.scrollView addSubview:currentViewController.view];

    self.scrollView.backgroundColor = [UIColor redColor];

    NSDictionary *numberItem = [contentList objectAtIndex:page];
    currentViewController.numberImage.image = [UIImage imageNamed:[numberItem valueForKey:IMAGE_KEY]];
    currentViewController.numberTitle.text = [numberItem valueForKey:NAME_KEY];
}
}

If I run the project, views with yellow background are displayed with no labels, nor image views.

As you can see, I set this background color programmatically. From my understanding, this means that the new view is correctly added to ViewController3, but it is not linked to the one configured on the storyboard.
Do you have any suggestions on how to solve this issue?

Thanks in advance,
yassa

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-12-30 08:15:09

您必须使用 [UIStoryboard instantiateViewControllerWithIdentifier:(NSString *)identifier] 实例化 PageViewController 实例。

http://developer.apple.com/库/ios/#documentation/UIKit/Reference/UIStoryboard_Class/Reference/Reference.html

You have to instantiate PageViewController instance using [UIStoryboard instantiateViewControllerWithIdentifier:(NSString *)identifier].

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboard_Class/Reference/Reference.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文