我是Iphone开发的新手(Xcode中的第3天),我正在尝试实现pageControl和scrollview,以便用户可以在各个页面之间滑动。我正在使用this教程,我无法弄清楚如何从nib文件加载/切换视图,而不仅仅是更改视图的背景颜色。非常感谢任何帮助。
我的代码
PageControlExampleViewController.m的修改重命名为NewsClass2
// Creates the color list the first time this method is invoked. Returns one color object from the list.
+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index {
if (__pageControlColorList == nil) {
__pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor magentaColor],
[UIColor blueColor], [UIColor orangeColor], [UIColor brownColor], [UIColor grayColor], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]];
}
//Changing views instead of colors, not working
+ (UIView *)pageControlViewWithIndex:(NSUInteger)index {
if (__pageControlViewrList == nil) {
__pageControlViewrList = [[NSArray alloc] initWithObjects:[[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil],
[[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlViewList objectAtIndex:index % [__pageControlViewList count]];
}
// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
self.view.backgroundColor = [NewsClass2 pageControlColorWithIndex:pageNumber];
//Setting View Not Working
self.view = [NewsClass2 pageControlViewWithIndex:pageNumber];
}
答案 0 :(得分:0)
欢迎来到Objective C.。
首先,您需要设置滚动视图的委托,如
//in .h file write
@interface ViewController : UIViewController<UIScrollViewDelegate>
// in viewDidLoad
self.scrollView.delegate = self;
然后写入UIScrollView的委托方法写...
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
然后对pageControl进行valueChange的IBAction,就像......
- (IBAction)changePage:(id)sender
{
int page = self.pageControlHelp.currentPage;
CGRect frame = self.scrollViewHelp.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[self.scrollViewHelp scrollRectToVisible:frame animated:YES];
}
你做完了.......
如果您对此有任何疑惑,请随时提出......