I am following this link CustomPageControlForPageViewController to implement SwipeView menu. In this project, there are only two ViewControllers which involves in the swipe transition. But my requirement is of three ViewControllers like. So I am not getting any idea that, how to implement the UIPageViewController delegate methods with three ViewControllers which ll handle the transition from one view to other. Here is the code from the given link for two ViewControllers.
swipe.h
#interface RootViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
#property (strong, nonatomic) UIPageViewController *pageViewController;
#property (strong, nonatomic) UIView *pageControlCustomView;
#end
swipe.m
#interface RootViewController () {
NSArray *viewControllers;
UIView *selectionBar;
int SELECTOR_WIDTH;
int SELECTOR_Y;
UIButton *leftButton;
UIButton *rightButton;
moveSelectorBarLeftBlockType moveSelectorBarLeft;
moveSelectorBarRightBlockType moveSelectorBarRight;
UIViewController *firstVC;
UIViewController *secondVC;
}
#end
#implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
SELECTOR_WIDTH = self.view.frame.size.width/2;
SELECTOR_Y = self.navigationController.navigationBar.frame.size.height + Y_OFFSET_BELOW_NAVBAR + HEIGHT;
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor blackColor];
// Create page view controller
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
self.pageViewController.delegate = self;
firstVC = [self.storyboard instantiateViewControllerWithIdentifier:#"firstViewController"];
secondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"secondViewController"];
viewControllers = #[firstVC];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if (self.pageViewController.viewControllers[0] == secondVC)
return firstVC;
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if (self.pageViewController.viewControllers[0] == firstVC)
return secondVC;
return nil;
}
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
if (completed) {
if (previousViewControllers[0] == secondVC) {
moveSelectorBarLeft();
}
else {
moveSelectorBarRight();
}
}
}
I recommend you to use your own array to store the order of your viewControllers.
Here is how I work with this delegate
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [self.viewControllers indexOfObject:viewController];
if (index == 0) { return nil;}
index--;
UIViewController *targetViewController = [self.viewControllers objectAtIndex:index];
return targetViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [self.viewControllers indexOfObject:viewController];
index++;
if (index >= self.viewControllers.count) { return nil;}
UIViewController *targetViewController = [self.viewControllers objectAtIndex:index];
return targetViewController;
}
Related
I'm following this tutorial to create a UIPageViewController:
appcoda UIPageViewController tutorial
However, the only difference in the code is I am using only 1 UIViewController and 1 UIPageViewController instead of 2 UIViewControllers and 1 UIPageViewController, and I've modified the code to allow me to accomplish this.
However, Xcode seems to crash in viewDidLoad in the code:
[self.view addSubview:_pageViewController.view];
Here is my code:
.h
#property (strong, nonatomic) UIPageViewController *pageViewController;
#property (strong, nonatomic) NSArray *pageImages;
#property (nonatomic) NSUInteger pageIndex;
#property (nonatomic) NSString *titleText;
#property (strong, nonatomic) IBOutlet UIImageView *imageFile;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Override point for customization after application launch.
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
self.pageImages = #[#"page1.png", #"page2.png", #"page3.png", #"page4.png"];
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
NSArray *viewControllers = #[ [self viewControllerAtIndex:0] ];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
// [self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
// [self.pageViewController didMoveToParentViewController:self];
}
- (id )viewControllerAtIndex:(NSUInteger)index
{
if ( ([self.pageImages count] == 0) || (index >= [self.pageImages count]) )
{
return nil;
}
self.imageFile = self.pageImages[index];
self.pageIndex = index;
return self;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = self.pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = self.pageIndex;
if (index == NSNotFound)
{
return nil;
}
index++;
if (index == [self.pageImages count])
{
return nil;
}
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.pageImages count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
I don't quite understand why it's crashing. Thanks for your help!
I have an app that presents a PageViewController tutorial scene. In my settings page I have a button that returns to the PageViewController, labeled IntroViewController.
When the tutorial first appears, it works fine. But when I go back, the dots at the base of the PageViewController disappear. Any idea why this happens?
Here is the IntroViewController.m
#import "IntroViewController.h"
#interface IntroViewController ()
#end
#implementation IntroViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Create the data model
_pageTitles = #[#"Over 200 Tips and Tricks", #"Discover Hidden Features", #"Bookmark Favorite Tip", #"Another one!"];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
_pageImages = #[#"IntroExpand.png", #"IntroSlow.png", #"IntroImport.png", #"IntroRename.png"];
}
else {
_pageImages = #[#"IntroExpand.png", #"IntroSlow.png", #"IntroImport.png", #"IntroRename.png"];
}
// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource = self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = #[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 5);
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
[self.view bringSubviewToFront:self.ready];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)ready:(id)sender { //this is the action that the user does to dismiss the tutorial and go to the first scene
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController =
[storyBoard instantiateViewControllerWithIdentifier:#"NavigationViewController"];
[self presentViewController:viewController animated:YES completion:nil];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.imageFiles = self.pageImages[index];
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.pageTitles count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
Here is the IntroViewController.h
#import <UIKit/UIKit.h>
#import "PageContentViewController.h"
#interface IntroViewController : UIViewController <UIPageViewControllerDataSource>
- (IBAction)ready:(id)sender;
#property (weak, nonatomic) IBOutlet UIButton *ready;
#property (weak, nonatomic) UIPageViewController *pageViewController;
#property (strong, nonatomic) NSArray *pageTitles;
#property (strong, nonatomic) NSArray *pageImages;
#end
And here is the button/action that takes the user from the settings page to the tutorial. I've tried different segues, modal, push, etc., and they all have the same problem of not displaying the dots when the user returns to the tutorial
- (IBAction)tutorialReplay:(id)sender {
UIStoryboard *storyBoard;
storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController =
[storyBoard instantiateViewControllerWithIdentifier:#"IntroViewController"];
[self presentViewController:viewController animated:YES completion:nil];
}
Let me know if I need to add more code to help. Thanks all.
In my custom UIPageViewController class:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.model = [[BSTCMWelcomingPageViewModel alloc] init];
self.dataSource = self.model;
self.delegate = self;
self.pageControl = [UIPageControl appearance];
self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
}
return self;
}
Then I programmatically set the current ViewController when a button is hit:
- (void)scrollToNext
{
UIViewController *current = self.viewControllers[0];
NSInteger currentIndex = [self.model indexForViewController:current];
UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
if (nextController) {
NSArray *viewControllers = #[nextController];
// This changes the View Controller, but PageControl doesn't update
[self setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
//Nothing happens!
[self.pageControl setCurrentPage:currentIndex];
//Error: _installAppearanceSwizzlesForSetter: Not a setter!
[self.pageControl updateCurrentPageDisplay];
}
}
If I can't do this with the UIPageControl that "belongs" to my UIPageViewController I will just try to make my own. But it would be nice if this was possible tho!
to update your UIPageControl indicator, you need to implement one data source method of UIPageViewController (the UIPageViewControllerDataSource method) :
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
This method is responsible for updating the page control indicator (when you use UIPageViewController). You only need to return the currentpage value in this method. The Method gets called by default when you use/make a call for setViewControllers on your custom UIPageViewController.
So the chunk of code that you need to write is:
- (void)scrollToNext
{
UIViewController *current = self.viewControllers[0];
NSInteger currentIndex = [self.model indexForViewController:current];
UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
if (nextController) {
NSArray *viewControllers = #[nextController];
// This changes the View Controller and calls the presentationIndexForPageViewController datasource method
[self setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return currentIndex;
}
Hope this solves your problem. :)
As mentioned in accepted answer, you need to implement
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
But for me it was enough to use it like this:
Objective-C:
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return [self.controllers indexOfObject:[pageViewController.viewControllers firstObject]];
}
Swift 3+:
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
guard let index = viewControllers?.index(of: (pageViewController.viewControllers?.first)!) else { return 0 }
return index
}
Without the need to remember the current index. Where self.controllers is an NSArray of UIViewControllers displayed in given UIPageViewController. I'm not sure how exactly your BSTCMWelcomingPageViewModel works, but it should be easy to adjust.
Xamarin/C# Solution
I had this problem in Xamarin, here is my version of #micromanc3r's solution:
public class PageViewControllerDataSource : UIPageViewControllerDataSource
{
UIViewController[] pages;
public PageViewControllerDataSource(UIViewController[] pages)
{
this.pages = pages;
}
...
public override nint GetPresentationIndex(UIPageViewController pageViewController)
{
return Array.IndexOf(pages, pageViewController.ViewControllers[0]);
}
}
SWIFT 4.2
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
guard let currentController = pageViewController.viewControllers?.first else {
return 0 }
guard let index = viewControllerList.index(of: currentController) else { return 0 }
return index
}
A page indicator will be visible if both methods are implemented, transition style is UIPageViewControllerTransitionStyleScroll and navigation orientation is UIPageViewControllerNavigationOrientationHorizontal. Both methods are called in response to a setViewControllers:... call, but the presentation index is updated automatically in the case of gesture-driven navigation.
(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0);: The number of items reflected in the page indicator.
(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController NS_AVAILABLE_IOS(6_0);: The selected item reflected in the page indicator.
The answer above, given by aansul, works.
Note : Don't forget to set the pageViewController's Transition style to Scroll instead of Page Curl. Otherwise it won't work.
// ViewController.h File
#import <UIKit/UIKit.h>
#import "PageContentViewController.h"
#interface ViewScreen : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
- (IBAction)Startwalkthrough:(id)sender;
#property(strong, nonatomic)UIPageViewController *pageViewController;
#property(strong, nonatomic)NSArray * pageTitles;
#property(strong,nonatomic)NSArray * pageImages;
#end
// ViewController.m File
#import "ViewScreen.h"
#interface ViewScreen ()
#end
#implementation ViewScreen
#synthesize pageTitles,pageImages;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
pageTitles =#[#"dianna",#"images",#"image",#"i1",#"hulk1",#"assasins"];
pageImages =#[#"dianna",#"images",#"image",#"i1",#"hulk1",#"assasins"];
self.pageViewController =[self.storyboard instantiateViewControllerWithIdentifier:#"PageViewController"];
self.pageViewController.dataSource=self;
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray * viewController =#[startingViewController];
[self.pageViewController setViewControllers:viewController direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:_pageViewController];
[self.view addSubview:_pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
-(IBAction)Startwalkthrough:(id)sender
{
PageContentViewController * startingViewController =[self viewControllerAtIndex:0];
NSArray * viewControllers =#[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.titletext = self.pageTitles[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
#pragma mark - Page View Controller Data Source
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index =((PageContentViewController *) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index--;
if (index ==[self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
return [self.pageTitles count];
}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
// Second File
PageViewController.h File
#import <UIKit/UIKit.h>
#interface PageContentViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *txtLabel;
#property (strong, nonatomic) IBOutlet UIImageView *backgroundImageView;
#property NSUInteger pageIndex;
#property NSString *titletext;
#property NSString * imageFile;
#end
// SecondFile.M File
#import "PageContentViewController.h"
#interface PageContentViewController ()
#end
#implementation PageContentViewController
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.backgroundImageView.image =[UIImage imageNamed:self.imageFile];
self.txtLabel.text =self.titletext;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
Make sure to set the currentIndex BEFORE you call setViewControllers
How can I dismiss/pop a UIPageViewController after the last view controller is swiped?
Basically want to make a tutorial style paging view with images, and to dismiss after the user swipes to the "next" page from the last.
- (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];
}
Here's simple solution I've used:
In
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
return empty controller for index == 5 and add this UIPageViewControllerDelegate method:
- (void)pageViewController:(UIPageViewController *)pageViewController
willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
EmailTutorialViewController *viewController =
[pendingViewControllers objectAtIndex:0];
if ([viewController index] == 5)
{
[self.navigationController popViewControllerAnimated:YES];
}
}
Within your UIPageViewController or viewController conforming to the UIPageViewControllerDelegate protocol:
// 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
I have three view controllers: Settings,Chatbox and QuickMsg.
I want to use UIPageViewController to switch between these three view controllers. For each view controller, I don't want to re-initiate the view controller between swiping pages since it contains user inputed data and I want to persist the data. I defined three view controllers as static variables.
The screen goes black when I swipe between pages. Where have I done wrong?
Thanks a lot in advance!
ChatboxController, SettingController and QuickMsgController are three UIViewController controllers.
PagingViewController.h
#import <UIKit/UIKit.h>
#import "ChatboxController.h"
#import "SettingController.h"
#import "PagingViewController.h"
#import "QuickMsgController.h"
#class ChatboxController;
#class SettingController;
#class QuickMsgController;
#interface PagingViewController : UIViewController<UIPageViewControllerDataSource>
#property (strong, nonatomic) UIPageViewController *pageController;
#property (assign, nonatomic) NSInteger index;
+(ChatboxController*) getChatboxController;
+(SettingController*) getSettingsController;
#end
PagingViewController.m
#import "PagingViewController.h"
#interface PagingViewController ()
#end
#implementation PagingViewController
static ChatboxController* chatboxViewController;
static SettingController* settingViewController;
static QuickMsgController* quickMessageViewController;
int defaultIndex=1;
NSArray *viewControllers;
+(ChatboxController*) getChatboxController
{
return chatboxViewController;
}
+(SettingController*) getSettingsController
{
return settingViewController;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
self.index--;
if(self.index<0)
{
self.index=0;
}
return [self viewControllerAtIndex:self.index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
self.index++;
return [self viewControllerAtIndex:self.index];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
NSLog(#"index:%d",index);
if(index==1)
{
return chatboxViewController;
}
else if (index==0){
return settingViewController;
}else if(index==2)
{
NSLog(#"Quick Message");
return quickMessageViewController;
}else{
NSLog(#"error here");
return nil;
}
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
// The number of items reflected in the page indicator.
return 3;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return defaultIndex;
}
- (void)viewDidLoad
{
[super viewDidLoad];
chatboxViewController= [self.storyboard instantiateViewControllerWithIdentifier:#"chatbox"];
settingViewController= [self.storyboard instantiateViewControllerWithIdentifier:#"settings"];
quickMessageViewController=[self.storyboard instantiateViewControllerWithIdentifier:#"quickmessages"];
self.index=defaultIndex;
// [self performSelector:#selector(loadingNextView) withObject:nil afterDelay:2.0f];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
NSArray *viewControllers = [NSArray arrayWithObject:chatboxViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Put your content view controllers in an NSArray:
#property (nonatomic, strong) NSArray *contentViewControllers;
Use:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = [self.contentViewControllers indexOfObject:viewController];
if (index == 0) {
return nil;
}
return self.contentViewControllers[index - 1];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = [self.contentViewControllers indexOfObject:viewController];
if (index >= self.contentViewControllers.count - 1) {
return nil;
}
return self.contentViewControllers[index + 1];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
return self.contentViewControllers.count;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
return 0;
}