问题
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(153,356,38,36) ];
pageControl.userInteractionEnabled =YES;
pageControl.numberOfPages = 2;
pageControl.currentPage = 1;
pageControl.enabled = TRUE;
[pageControl setHighlighted:YES];
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
}
- (IBAction) changePage:(id)sender
{
}
I'm programmatically creating page control and i want to display new view controllers on click of page control. How i need to implement this changePage method? Can anyone help?
回答1:
You can show two views instead of showing two different view controller. You can keep first dot selected and show first view and have next view out of screen, to its right. When user taps second dot, make UIView animation similar to pushing in UINavigationController. Thus, you do push and pop with UIView animation.
If you want to show view controllers, then the page control needs to be shown in both the view controller, so that user can switch from one to another. In such case, you need to have the page control in a view, added in the main window, so that its visible everywhere.
回答2:
The easiest way to program a method to change pages would be the following:
- (IBAction)changePage:(id)sender {
CGrect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
}
EDIT: if you are trying to simply change the view controller by clicking the dots, you will need to set your page up so that the main view has a UIPageControl at the bottom and another UIView (we will call this controllerView) above it taking up most of the screen, but not overlaying the page control.
You will also want PageOne *pageOneController;
and PageTwo *pageTwoController;
in your header file. This will help prevent memory leaks.
So when you select another page, you'll call your changePage
method
- (IBAction)changePage:(id)sender {
if (sender.currentPage == 1) {
// make sure only one instance exists at a time so there aren't any memory leaks;
if (pageOneController != nil) {
pageOneController = nil;
[pageOneController release];
}
// load up page one;
pageOneController = [[PageOne alloc] initWithNibName:@"PageOneNib" bundle:nil];
// set this as the primary view;
controllerView = viewController.view;
} else {
// do the same for your other page;
}
}
This should do the trick for you
来源:https://stackoverflow.com/questions/5960177/problem-in-uipagecontrol