How to implement UIPageViewController that utilizes multiple ViewControllers

后端 未结 4 787
攒了一身酷
攒了一身酷 2020-11-28 22:49

I\'ve been working on a simple test app to learn the ins and outs of the UIPageViewController. I have it working but I\'m not convinced my execution is the best way. I hop

4条回答
  •  情话喂你
    2020-11-28 23:08

    @interface ViewController ()
    {
        NSMutableArray * StoryboardIds;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        StoryboardIds = [[NSMutableArray alloc]init];
        [StoryboardIds addObject:@"vc1"];
        [StoryboardIds addObject:@"vc2"];
    
        UIViewController *selectedController = [self viewControllerAtIndex:0];
    
        self.ProfilePageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageControl"];
        self.ProfilePageViewController.dataSource = self;
    
        _viewcontrollers = [NSMutableArray new];
    
        [_viewcontrollers addObject:selectedController];
    
        [self.ProfilePageViewController setViewControllers:_viewcontrollers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
        // Change the size of page view controller
        self.ProfilePageViewController.view.frame = CGRectMake(0, 0, self.bodypage.frame.size.width, self.bodypage.frame.size.height);
    
        [self addChildViewController:self.ProfilePageViewController];
        [self.ProfilePageViewController.view setFrame:self.bodypage.bounds];
        [self.bodypage addSubview:self.ProfilePageViewController.view];
        [self.ProfilePageViewController didMoveToParentViewController:self];
    
    }
    - (UIViewController *)viewControllerAtIndex:(NSUInteger)index
    {
        if (([StoryboardIds count] == 0) || (index >= [StoryboardIds count])) {
            return nil;
        }
    
        if (index == 0) {
            vc1 *fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"vc1"];
            fvc.Pageindex = index;
            if ([_viewcontrollers count]) {
                [_viewcontrollers replaceObjectAtIndex:0 withObject:fvc];
            }
            return fvc;
        }
        else
        {
            vc2 *fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"vc2"];
            fvc.Pageindex = index;
            if ([_viewcontrollers count]) {
                [_viewcontrollers replaceObjectAtIndex:0 withObject:fvc];
            }
            return fvc;
        }
    
    }
    
    -(NSUInteger)indexofViewController
    {
        UIViewController *currentView = [self.ProfilePageViewController.viewControllers objectAtIndex:0];
    
        if ([currentView isKindOfClass:[vc2 class]]) {
            return 1;
        }
        else{
            return 0;
        }
    
    }
    
    
    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
        NSUInteger index = [self indexofViewController];
    
        if ((index == 0) || (index == NSNotFound)) {
            return nil;
        }
    
        index--;
        return [self viewControllerAtIndex:index];
    }
    
    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
        NSUInteger index = [self indexofViewController];
    
        if (index == NSNotFound) {
            return nil;
        }
    
        index++;
    
        if (index == [StoryboardIds count]) {
            return nil;
        }
        return [self viewControllerAtIndex:index];
    }
    

提交回复
热议问题