在刷过最后一个视图控制器后,如何解除/弹出UIPageViewController?

时间:2014-01-23 17:25:05

标签: ios uipageviewcontroller

如何在刷过最后一个视图控制器后解除/弹出UIPageViewController? 基本上想要用图像制作教程样式的分页视图,并在用户从最后一页滑到“下一页”后解散。

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    NSUInteger index = [(EmailTutorialViewController *)viewController index];


    index++;

    if (index == 5) {
        [self.navigationController popViewControllerAnimated:YES]; // this doesn't do anything
        return nil;
    }

    return [self viewControllerAtIndex:index];
}

2 个答案:

答案 0 :(得分:10)

这是我用过的简单解决方案:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

index == 5返回空控制器并添加此UIPageViewControllerDelegate方法:

- (void)pageViewController:(UIPageViewController *)pageViewController 
willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
    EmailTutorialViewController *viewController = 
                [pendingViewControllers objectAtIndex:0];
    if ([viewController index] == 5) 
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

答案 1 :(得分:0)

符合UIPageViewControllerDelegate协议的UIPageViewController或viewController:

// UIPageViewController.h
@interface UIPageViewController

@property (nonatomic, assign) NSInteger pageIndex;

@end

// UIPageViewController.m
@implementation UIPageViewController

/* ... */

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Start with animation from the presenting view/item.
    UIViewController *singlePageVC = [UIViewController viewControllerWithPageIndex:self.pageIndex];
    singlePageVC.view.alpha = 0.0f;
    if (singlePageVC != nil) {
        self.dataSource = self;
        self.delegate = self;

        // Add singlePageVC to the page view controller with the initial pageIndex.
        [self setViewControllers:@[ singlePageVC ]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO
                      completion:nil];
    }
}

#pragma mark - UIPageViewControllerDelegate

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = singlePageVC.pageIndex;
    // This will add a page before the current view controller.
    // Even if the index is 0, a new (dismissing) view controller will be added here.
    return [UIViewController viewControllerWithPageIndex:index - 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = singlePageVC.pageIndex;
    // This will add a page after the current view controller.
    // Even if the index is at your array bounds (i.e., 5), a new (dismissing) view controller will be added here.
    return [UIViewController viewControllerWithPageIndex:index + 1];
}

/* ... */

@end

// UIViewController+PageIndex.h
extern NSString * const UIViewControllerShouldDismissFromPageViewControllerNotification;

@interface UIViewController (PageIndex)

@property (nonatomic, assign) NSInteger pageIndex;

- (instancetype)initWithPageIndex:(NSInteger)pageIndex;
+ (instancetype)viewControllerWithPageIndex:(NSInteger)pageIndex;

@end

// UIViewController+PageIndex.m
NSString * const UIViewControllerShouldDismissFromPageViewControllerNotification = @"UIViewControllerShouldDismissFromPageViewControllerNotification";

@implementation UIViewController (PageIndex)

// Initialize with the current item's index.
- (instancetype)initWithPageIndex:(NSInteger)pageIndex
{
    self = [super init];
    if (self) {
        _pageIndex = pageIndex;
    }
    return self;
}

+ (instancetype)viewControllerWithPageIndex:(NSInteger)pageIndex
{
    return [[self alloc] initWithPageIndex:pageIndex];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Check for pageIndex out of bounds of your array.
    // This will be either less than 0 or greater or equal to your array count.
    if (self.pageIndex < 0 || self.pageIndex >= array.count) { // Replace with your global or passed array here.
        // Post dismiss notification here.
        [[NSNotificationCenter defaultCenter]
        postNotificationName:UIViewControllerShouldDismissFromPageViewControllerNotification
        object:nil];
        return;
    }
}

@end

// View controller than manages your UIPageViewController.

@implementation YourViewController

/* ... */

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter]
    addObserver:self selector:@selector(dismissPageViewController:)
    name:UIViewControllerShouldDismissFromPageViewControllerNotification
    object:nil];
}

- (void)dismissPageViewController:(NSNotification *)notification
{
    // Dismiss your page view controllers here.
    [self.pageViewController dismissViewController];
    self.pageViewController = nil;

    // Or remove the page view controllers from its parent view controller,
    [self.pageViewController willMoveToParentViewController:nil];
    [self.pageViewController removeFromParentViewController];
    [self.pageViewController didMoveToParentViewController:nil];

    // Or its view from its super view.
    [self.pageViewController.view removeFromSuperview];
    self.pageViewController = nil;
}

/* ... */

@end