Using 2 different view controllers in UIPageViewController as pages

So, I searched everywhere and cannot find what I am looking for. Basically, I have 3 different view controllers in my application. (MainViewController, ChildViewController, TwitterViewController) At first I practiced using only the ChildViewController and updating the shortcut to tell which page you are on. However, I want to add to TwitterViewController as "Page 2" and save ChildViewController as "Page 1".

Currently, I have a MainViewController installed as a Delegate and a DataSource, and return the code with only one "ChildViewController" as different pages. Please let me know if this is not done. I will try to repeat if necessary. Below is my code.

MainViewController.m

#import "MainViewController.h"
#import "ChildViewController.h"
#import "TwitterViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    ChildViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [(ChildViewController *)viewController index];

    if (index == 0) {
        return nil;
    }

    index--;

    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [(ChildViewController *)viewController index];

    index++;

    if (index == 2) {
        return nil;
    }

    return [self viewControllerAtIndex:index];
}

- (ChildViewController *)viewControllerAtIndex:(NSUInteger)index {
    ChildViewController *first = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
    first.index = index;

    return first;
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return 2;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

ChildViewController.m

#import "ChildViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ChildViewController ()

@end

@implementation ChildViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.screenNumber.text = [NSString stringWithFormat:@"This is Page #%d", self.index];
    self.screenNumber.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];
    self.screenNumber.layer.cornerRadius =10;    
}

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

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

TwitterViewController is by default and not yet written. Just try it with an empty ViewController for now.

Any help would be great! Thanks guys!

+4
source share
1 answer

. , UIViewController , "index" . ChildViewController TwitterViewContoller. (ChildViewController *)viewControllerAtIndex:(NSUInteger)index (BaseViewController *)viewControllerAtIndex:(NSUInteger)index.

.

+4

All Articles