I would like to use the page control to switch between multiple viewControllers. I have the following viewController, the nib associated with it contains a UIScrollView and a UIPageControl. I placed the scroll over the page control using Xcode IB so that both controls are visible, and this is the .h file:
@interface NewForm : UIViewController <UIScrollViewDelegate> { BOOL pageControlUsed; } @property (nonatomic, retain) IBOutlet UIScrollView *scrollView; @property (nonatomic, strong) IBOutlet UIPageControl *pageControl; @property (nonatomic, retain) NSMutableArray *viewControllers; - (IBAction)changePage:(id)sender; @end
The view and output of scrollView is associated with the owner of the file, as well as the scroll delegate. pageControl outlet and changePage are associated with UIPageControl.
This is the .m file (only relevant methods):
@implementation STNewAccountTest @synthesize scrollView, viewControllers, pageControl; - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *controllers = [[NSMutableArray alloc] init]; [controllers addObject:[[Page1 alloc] initWithNibName:@"Page1" bundle:nil]]; [controllers addObject:[[Page2 alloc] initWithNibName:@"Page2" bundle:nil]]; self.viewControllers = controllers; 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; self.pageControl.currentPage = 0; self.pageControl.numberOfPages = numberOfPages; [self loadScrollViewWithPage:0]; } - (void)loadScrollViewWithPage:(int)page { if ((page < 0) || (page >= numberOfPages)) return; Page1 *controller1 = nil; Page2 *controller2 = nil; if (page == 0) { controller = [self.viewControllers objectAtIndex:page]; if (controller == nil) { controller = [[Page1 alloc] initWithNibName:@"Page1" bundle:nil]; [self.viewControllers replaceObjectAtIndex:page withObject:controller]; } } if (page == 1) { controller = [self.viewControllers objectAtIndex:page]; if (controller == nil) { controller = [[Page2 alloc] initWithNibName:@"Page2" bundle:nil]; [self.viewControllers replaceObjectAtIndex:page withObject:controller]; } } if (controller.view.superview == nil) { CGRect frame = self.scrollView.frame; frame.origin.x = frame.size.width * page; frame.origin.y = 0; controller.view.frame = frame; [self.scrollView addSubview:controller.view]; } } - (void)scrollViewDidScroll:(UIScrollView *)sender { if (pageControlUsed) { return; }
When I launch the application, what I see is that pageviews occupy the entire screen, and I can navigate the pages using the scroll scroll function, but the page control and its points do not appear, What can I lose?
Thanks!
source share