UIPageControl中的问题 [英] problem in UIPageControl
问题描述
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
{
}
我正在以编程方式创建页面控件,并且希望在单击页面控件时显示新的视图控制器.我需要如何实现此changePage方法?有人可以帮忙吗?
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?
推荐答案
编写更改页面方法的最简单方法是:
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];
}
如果您只是想通过单击点来简单地更改视图控制器,则需要设置页面,以便主视图在底部具有UIPageControl,在上方具有另一个UIView(我们将其称为controllerView)它占据了屏幕的大部分,但没有覆盖页面控件.
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.
您还将在头文件中使用 PageOne * pageOneController;
和 PageTwo * pageTwoController;
.这将有助于防止内存泄漏.
You will also want PageOne *pageOneController;
and PageTwo *pageTwoController;
in your header file. This will help prevent memory leaks.
因此,当您选择其他页面时,将调用 changePage
方法
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;
}
}
这应该为您解决问题
这篇关于UIPageControl中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!