Professional Documents
Culture Documents
AppCoda
Your iOS Programming Community
About
Tutorials
Course
Book
Game Kit
Marketplace
Forum
Contact
Okay, lets move on. In the next screen enter PageViewDemo as the product name and set com.appcoda in the
company identifier field. Select iPhone for the Devices option. Press next and create the project.
For this project, the original view controller is used as the root view controller for holding the page view controller.
The view controller youve just added will be used for displaying the page content. Throughout the article, we refer
the original view controller as the root view controller and the other view controller as page content controller.
You may wonder why we just add a single view controller for 4 pages of content. Shouldnt we use four view
controllers instead of one? As you can see from the final deliverable, the walkthrough screens are very similar. Its
better to share the same view controller for different screens.
Next, assign a Storyboard ID for the page view controller and the page content controller. You can simply select the
controller and set the ID under Identity Inspector. Set the Storyboard ID of the page view controller as
PageViewController and name the ID of the page content controller as PageContentController. Later well refer to
these IDs in our code.
By default, the transition style of the page view controller is set as Page Curl. The page curl style is perfect for book
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/[3/21/2014 9:30:08 PM]
apps. For walkthrough screens, we prefer to use scrolling style. So change the transition style to Scroll under Attribute
Inspector.
Well design the user interface of page content view controller. Drag an image view and a label into the controller.
Youre free to change the font type and size. But your view controller should be similar to the below screenshot.
For the default view controller, add a Start again button and put it at the below of the screen.
Go back to Storyboard. Select the page content view controller and set the custom class to
PageContentViewController under Identify Inspector.
Next, well create outlets for the image view and label. Switch to the Assistant Editor and make sure the
PageContentViewController.h is opened. Control and drag from the image view to the PageContentViewController.h
and create an IBOutlet. Set the name as backgroundImageView for the image view. For the label, set the name of the
outlet as titleLabel.
#import <UIKit/UIKit.h>
@interface PageContentViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end
Next, select the root view controller and make sure the ViewController.h is opened. Create an action for the Start
again button and name the action as startWalkthrough.
Okay, weve completed the design of user interface and created all the outlets. Lets move onto the implementation of
the view controller classes.
The pageIndex stores the current page index (or page number). The view controller is designed to display an image
and a title. So we create two parameters for passing the title text and image file. Next, open ViewController.m and
change the viewDidLoad: method:
1
2
3
4
5
6
7
- (void)viewDidLoad
{
[super viewDidLoad];
self.backgroundImageView.image = [UIImage imageNamed:self.imageFile];
self.titleLabel.text = self.titleText;
}
In order to make UIPageViewController work, we must adopt the UIPageViewControllerDataSource protocol. The
data source for a page view controller is responsible for providing the content view controllers on demand. By
implementing the data source protocol, we tell the page view controller what to display for each page.
In this case, we use the ViewController class as the data source for the UIPageViewController instance. Therefore it is
necessary to declare the ViewController class as implementing the UIPageViewControllerDataSource protocol.
The ViewController class is also responsible to provide the data of the page content (i.e. images and titles). Open the
ViewController.h. Modify the @interface declaration, add a new property to hold the UIPageViewController, as well
as, properties for both images and titles:
1
2
3
4
5
6
7
8
9
10
11
#import <UIKit/UIKit.h>
#import "PageContentViewController.h"
@interface ViewController : UIViewController <UIPageViewControllerDataSource>
- (IBAction)startWalkthrough:(id)sender;
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSArray *pageTitles;
@property (strong, nonatomic) NSArray *pageImages;
@end
In the ViewController.m, initialize the pageTitles and pageImages in the viewDidLoad method:
1 - (void)viewDidLoad
2 {
3
4
5
6
7
[super viewDidLoad];
_pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark F
_pageImages = @[@"page1", @"page2", @"page3", @"page4", @"page5"];
}
Note: You can download image files from here and add them into the Xcode project.
We have created the data model for the page content. Next, we have to implement at least two methods of the
UIPageViewControllerDataSource protocol:
viewControllerAfterViewController provides the view controller after the current view controller. In other
words, we tell the app what to display for the next screen.
viewControllerBeforeViewController provides the view controller before the current view controller. In other
words, we tell the app what to display when user switches back to the previous screen.
Add the following lines of code before the end of the ViewController.m file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The above methods are very straightforward. First, we get the current page index. Depending the method, we simply
increase/decrease the index number and return the view controller to display. Of course, we have to verify if we have
reached the boundaries of the pages and return nil in that case.
As you may notice, we havent created the viewControllerAtIndex: method. It is a helper method that is designed to
create the page content view controller on demand. It takes in the index parameter and creates the corresponding page
content controller.
In the ViewController.m, add the helper method:
1
2
3
4
5
6
7
8
9
(PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
10
11
12
13
14
pageContentViewController.titleText = self.pageTitles[index];
pageContentViewController.pageIndex = index;
return pageContentViewController;
Recalled that we have set a storyboard ID for the view controllers when designing the user interface. The ID is used as
reference for creating the view controller instance. To instantiate a view controller in storyboard, you can use the
instantiateViewControllerWithIdentifier: method with a specific storyboard ID.
1 PageContentViewController *pageContentViewController = [self.storyboard instantiateViewCont
To display a page indicator, you have to tell iOS the number of pages (i.e. dots) to display in the page view controller
and which page must be selected at the beginning. Add the following two methods at the end of the ViewController.m
file:
1
2
3
4
5
6
7
8
9
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewControl
{
return [self.pageTitles count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewControl
{
return 0;
}
Again the above code is very straightforward. We simply tell iOS that we have the total number of pages to display in
the page view controller and the first page should be selected by default.
Note: You must implement both methods in order to display the page indicator. Also the page indicator only works in
scroll transition mode.
(void)viewDidLoad
[super viewDidLoad];
// Create the data model
_pageTitles = @[@"Over 200 Tips and Tricks", @"Discover Hidden Features", @"Bookmark
_pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];
13
14
15
16
Lets see what the method does. We first create the PageViewController instance. Next we specify the data source, in
this case it is the class itself. We then create the first page content controller, add it to an array of controllers and
assign it to the page view controller for display.
Lastly, we change the size of the page view controller and add the page controller view to the current view.
(IBAction)startWalkthrough:(id)sender {
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControl
Run the app again. The app will bring you back to the first page when tapping the Start again button.
Summary
In this tutorial, we cover the basics of UIPageViewController and demonstrate how to implement the controller by
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/[3/21/2014 9:30:08 PM]
using Storyboard. The UIPageViewController is a very handy class for implementing walkthrough screens in your app.
That said, the usage of UIPageViewController is unlimited. You can use it to display whatever information as you like
such as pages of web view. The UIPageViewController is highly configurable. This tutorial only covers the scroll
transition style. But dont you know that you can easily use the class to build a simple book app? Simply change the
transition style from scroll to page curl and see what youll get. So dont stop here. Try to change the available options
and learn about UIPageViewController.
For your complete reference, you can download the Xcode project from here. As always, please leave us comment and
share your thought.
Tweet
Filed Under: Course, Intermediate, Tutorials Tagged With: iOS Programming, UIPageViewController, Walkthrough
Screen
Get Free Chapters of Our AppCoda Book
The Learn iOS 7 Programming from Scratch is a 400-page eBook written for beginners with little or even no
programming experience. It is based on the tutorials of our popular programming course. The book starts with the
basics and walks you through the process to create iOS apps using iOS 7 SDK and Xcode 5. Want to learn more?
Search
Recent Discussions
Search Bar
Create a cross-platform mobile messaging app
How To Select The Image From Library To Parse To Twitter API?
Tutorial #3
How I can implement a SearchBar in a tableview with sections?
How to Remove Past events from iCalendar in ios
How to edit Docs,xlsx,pdf, in ios
Beginner with no previous experience seeking advice
Data passing through AppDelegate in iphone
Sqlite3
Recent Posts
iOS Programming 101: Adding Section and Index List in UITableView
Working With Background Fetch in iOS 7
Working with Game Center and Game Kit Framework
Announcing Our First AppCoda Book Learn iOS 7 Programming from Scratch
Understanding Multipeer Connectivity Framework in iOS 7 Part 2
Connect With Us
Twitter
Facebook
RSS
Google Plus
About AppCoda
About
Tutorials
Course
Book
http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/[3/21/2014 9:30:08 PM]
Game Kit
Marketplace
Forum
Contact
Latest Comments
John on How To Create UIPageViewController Using Storyboard
Don on Adding Local Notifications in Your iOS App
Gabriel Theodoropoulos on Working With Background Fetch in iOS 7
Rasmus on iOS Programming 101: How To Get the User Location in iPhone App
Working with Background Fetch in iOS 7 | Programming Tutorial | MUSCLE CODER on Working With
Background Fetch in iOS 7
Connect With Us
Twitter
Facebook
RSS
Google Plus
Email Newsletter
Sign up to receive free tutorial and to hear what's going on with AppCoda!
Subscribe