UIScrollView如何与StoryBoard一起使用并显示不同的UIViewControllers? [英] How does UIScrollView work with StoryBoard and show different UIViewControllers?

查看:88
本文介绍了UIScrollView如何与StoryBoard一起使用并显示不同的UIViewControllers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个StoryBoard。在那里有一个UIViewController - 我已经把UIScrollView和UIPageControl。在每个页面上(比方说我有3个)我试图加载一个UIViewController(显示不同的标签和表值)。

I have a StoryBoard. In that there is a UIViewController - on that I have put UIScrollView and UIPageControl. And on each page (say I have 3) I am trying to load a UIViewController (that shows different labels and table values).

问题 - 我能够加载所有内容向上但标签和表格不可见。当我尝试更改每个页面的背景颜色时 - 它确实会发生变化。我已经研究了各种帖子和代码,但似乎没有任何工作/使用StoryBoard显示UIScrollView(使用UIViewController显示标签和表格)。

Problem - I am able to load everything up but the label and table is not visible. Thou when I try to change the background color for each page - it does gets changed. I have researched various posts and code but nothing seems to work / show UIScrollView with StoryBoard (with UIViewController displaying labels and table).

我可以通过加载来执行此操作一个单独的XIB文件,但整个segue被中断。
请参阅下面的代码。

I am thou able to do this with loading a separate XIB file but then the whole segue gets interrupted. Please see my code below.

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];

//ScrollView and PageControl

// Create view controllers placeholder array
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfPages; i++) 
{
    [controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;    
// Set Scroll View
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self; 
// Set Page Control
pageControl.numberOfPages = numberOfPages;
pageControl.currentPage = 0;
// Load the visible and next page
[self loadScrollViewWithPage:0];
[self loadScrollViewWithPage:1];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed)
{
    // To know if scroll is valid - from Page Control
    return;
}

// Changes Page Control indicator
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;

// Load the visible, previous and next pages
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{
pageControlUsed = NO;
}

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

// Load new Controller
ReportsDetailViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) 
{
    controller = [[ReportsDetailViewController alloc] init];
    controller.pageNumber = [NSNumber numberWithInt:page];
    [viewControllers replaceObjectAtIndex:page withObject:controller];
}   
if (nil == controller.view.superview) 
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    controller.view.frame = frame;
    [scrollView addSubview:controller.view];
}
}


推荐答案

尝试像这样创建你的ViewControllers:

Try to create your ViewControllers like this:

YourViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourViewController"];

您可以在属性检查器中设置视图控制器的标识符。

You can set the identifier of your view controller in the attribute inspector.

我今天遇到了同样的问题,这对我有用。

I had the same problem today and this did the trick for me.

这篇关于UIScrollView如何与StoryBoard一起使用并显示不同的UIViewControllers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆