我使用以下代码实现UIPageViewController
。如何使用UIButton点击事件移动下一页和上一页?
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentView*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentView *) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [arrCarStatus count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
答案 0 :(得分:3)
您需要在UIPageViewController
点击时调用UIButton
种方法。这是你需要做的。为UIPageViewController
- (void)changePage:(UIPageViewControllerNavigationDirection)direction {
NSUInteger pageIndex = ((ViewControllerContainingUIImageView *) [_pageViewController.viewControllers objectAtIndex:0]).index;
if (direction == UIPageViewControllerNavigationDirectionForward)
{
pageIndex++;
}
else
{
pageIndex--;
}
initialViewController = [self viewControllerAtIndex:pageIndex];
if (initialViewController == nil) {
return;
}
[_pageViewController setViewControllers:@[initialViewController]
direction:direction
animated:YES
completion:nil];
}
现在在UIButton
点击提供方向前进/后退
-(void)backSlide
{
[self changePage:UIPageViewControllerNavigationDirectionReverse];
}
-(void)forwardSlide
{
[self changePage:UIPageViewControllerNavigationDirectionForward];
}
答案 1 :(得分:2)
- (void)viewDidLoad
{
[super viewDidLoad];
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pvc.view.frame = CGRectInset(self.view.bounds, 200, 200);
[self.view addSubview:self.pvc.view];
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
-(UIViewController*)randomVC
{
UIViewController *vc = [[UIViewController alloc] init];
UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
vc.view.backgroundColor = color;
return vc;
}
- (IBAction)previousButtonPressed:(id)sender {
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
- (IBAction)nextButtonPressed:(id)sender {
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
试试这个可以帮助你。我做了全新的演示。
答案 2 :(得分:0)
您可以使用名为
的页面视图控制器的委托方法- (void)changePage:(UIPageViewControllerNavigationDirection)direction
只需简单检查下面提到的条件。
if (direction == UIPageViewControllerNavigationDirectionForward)
{
pageIndex++;
}
else
{
pageIndex--;
}