我是 Iphone 开发的新手(Xcode 中的第 3 天),我正在尝试实现 pageControl 和 scrollview,以便用户可以在各个页面之间滑动。我正在使用本教程,但我不知道如何从 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];
}