0

我是 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];
}
4

1 回答 1

0

欢迎来到目标 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];
}

而你已经做到了......

如果您对此有任何困惑,请随时询问......

于 2013-03-21T09:00:22.517 回答