So I have looked everywhere and can't seem to find what I'm looking for. Basically, I have 3 different View Controllers in my app. (MainViewController, ChildViewController, TwitterViewController) At first I was practicing using just the ChildViewController and updating a label to tell what page you are on. However, I want to add in TwitterViewController as a "Page 2" and keep ChildViewController as "Page 1".
At the moment I have the MainViewController set as the Delegate and DataSource and put the code back to just using the one "ChildViewController" as the different pages. Please let me know if this doesn't make since. I will try to reiterate if needed. 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
The TwitterViewController is default and has not been written yet.. Just trying it with a blank ViewController at the moment.
Any help would be great!! Thanks guys!