How does UIScrollView work with StoryBoard and show different UIViewControllers?

I have a StoryBoard. There is a UIViewController in this - on this I put UIScrollView and UIPageControl. And on every page (let's say I have 3) I try to load a UIViewController (which shows different labels and table values).

Problem. I can download everything, but the shortcut and table are not visible. When I try to change the background color for each page, it changes. I studied various messages and code, but nothing works / will show UIScrollView with StoryBoard (with UIViewController displaying labels and table).

I can do this by loading a separate XIB file, but then the entire segment will be interrupted. 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]; } } 
+4
source share
1 answer

Try creating your ViewControllers as follows:

 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 it helped me.

+3
source

Source: https://habr.com/ru/post/1392326/


All Articles