IOS 5: An example of a Uipageviewcontroller program

Source: Internet
Author: User
Tags uikit

Original: Http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application

When you create a new project in Xcode, you can select the Page-based Application project template. You can use this template to create a "page-based" application that displays different pages in each month of the year 1. Oddly, this is the only instance-based template that Xcode provides, not the application's basic framework. This is useful when you start learning, but unless you really need a 12-page program that shows a 1-year different month, you'll have to remove something from the template for other purposes.

In fact, in addition to using Xcode's "page-based Application" template, we can also use the single View application template, except that we need to implement page-based behavior. There are two advantages to this. First, not using the Page-base template allows us to better understand the implementation details of Uipageviewcontroller. Second, this approach is more straightforward than modifying page-based application templates.

Create a project

Start Xcode, create a new iOS single view Application project, and verify that you do not use the Using Storyboard option.

Create Contentviewcontroller

This example displays multiple pages using a single view controller instance. The view contains a UIWebView that displays different HTML content based on the currently selected page. The Viewcontroller class has a data object property that holds the HTML for the view.

Choose Fileànewànewfile ... menu, new Uiviewcontroller subclass, named Contentviewcontroller (tick the "with XIB ..." option). Open ContentViewController.h, add an exit for the WebView object and the data object, and connect.

#import <UIKit/UIKit.h>

Then, open contentviewcontroller.xib, drag a UIWebView component onto the view:

Right-drag a connector from file's Owner to the Web View object and select WebView exit.

Edit the Contentviewcontroller.m file. Whenever the user pages, the Uipageviewcontroller data source method creates a new Contentviewcontroller instance and then sets its DataObject property to the corresponding HTML. In the Viewwillappear method of Contentviewcontroller, we assign the DataObject property to the WebView object. To do this, we have added the necessary @synthesize statements and modified the Viewwillappear method:

-(void) Viewwillappear: (BOOL) Animated {
[Super viewwillappear:animated];
[WebView Loadhtmlstring:dataobject

Next, let's implement the data model.

Creating a data Model

The data model in this example is an array of strings. Each string is a different HTML content. The Pageappviewcontroller class is the data source for the Uipageviewcontroller object. The class contains a Nsarry and a Uipageviewcontroller object, and implements the Uipageviewcontrollerdatasource protocol. Open PageAppViewController.h, import the contentViewController.h header file, and add the necessary declarations:

@interface Pageappviewcontroller:uiviewcontroller <UIPageViewControllerDataSource> {
Uipageviewcontroller *pagecontroller;

Finally, add a method in PAGEAPPVIEWCONTROLLER.M to add the HTML string to the array. This method is then called in the Viewdidload method.

@synthesize Pagecontroller, PageContent;
.
-(void) Createcontentpages {
Nsmutablearray *pagestrings = [[Nsmutablearray alloc] init];
for (int i = 1; i < one; i++)
{
NSString *contentstring = [[NSString alloc]
initwithformat:@ "
[Pagestrings addobject:contentstring];
}
}
.
-(void) Viewdidload {
[Super Viewdidload];

Now, we already have a content view controller and a data model, which extracts the contents of each page through the source method. The next step is to implement these data source methods. As described in "Implementinga page based IOS 5 iPhone application using Uipageviewcontroller", the Uipageviewcontroller instance requires a data source, There are two data source methods, one is to return the view controller after the currently displayed view controller, and the other is to return the Viewcontroller before the view controller that is currently displayed. Because Pageappviewcontroller plays the data source for Page view controller, you need to include both methods (and two other convenient methods) in PAGEAPPVIEWCONTROLLER.M. First of all, let's look at these 2 convenient methods:

-(Contentviewcontroller *) Viewcontrolleratindex: (Nsuinteger) Index {
Return The Data View controller for the given index.
(Index >= [self.pagecontent count])) {
return nil;
}
Create a new view controller and pass suitable data.
[[Contentviewcontroller Alloc]
Bundle:nil];
Dataviewcontroller.dataobject =
[Self.pagecontent Objectatindex:index];
-(Nsuinteger) Indexofviewcontroller: (Contentviewcontroller *) Viewcontroller {
}
.

Viewcontrolleratindex: The method first checks whether the valid pages are <0 (the user cannot go back to page 1th) or the number of pages to retrieve has exceeded the actual count of the PageContent array. If the index parameter is valid, create a new Contentviewcontroller instance and set the DataObject property to the corresponding PageContent entry (HTML string).

The Indexofviewcontroller method specifies a viewcontroller as a parameter and returns the index of this viewcontroller. It uses the DataObject property of Viewcontroller to retrieve its index in the pagecontent array element.

Now look at two data source methods. They use these two convenient methods to return the view controller of the current View controller "before" and "after":

-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerbeforeviewcontroller: (Uiviewcontroller *) Viewcontroller {
Nsuinteger index = [self Indexofviewcontroller:
(Contentviewcontroller *) Viewcontroller];
if (index = = 0) | | (index = = nsnotfound)) {
return nil;
}
index--;
-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerafterviewcontroller: (Uiviewcontroller *) Viewcontroller {
Nsuinteger index = [self Indexofviewcontroller:
(Contentviewcontroller *) Viewcontroller];
if (index = = nsnotfound) {
return nil;
}
index++;
if (index = = [Self.pagecontent count]) {
return nil;
}
}

The next step is to create and instantiate the Uipageviewcontroller class.

Instantiate Uipageviewcontroller

The next step is to create the Uipageviewcontroller instance and initialize it. This process only needs to be done once, so write the code in the Pageappviewcontroller Viewdidload method. Open pageappviewcontroller.m file modified viewdidload:

-(void) Viewdidload {
[Super Viewdidload];
[Self createcontentpages];
Nsdictionary *options =
[Nsdictionary Dictionarywithobject:
[NSNumber
Numberwithinteger:uipageviewcontrollerspinelocationmin]
Forkey:uipageviewcontrolleroptionspinelocationkey];
Self.pagecontroller = [[Uipageviewcontroller alloc] Initwithtransitionstyle:uipageviewcontrollertransitionstylepag Ecurl Navigationorientation:uipageviewcontrollernavigationorientationhorizontal
Options:options];
Pagecontroller.datasource = self;
[[Pagecontroller view] setframe:[[self view] bounds];
Contentviewcontroller *initialviewcontroller =
[Self viewcontrolleratindex:0];
Nsarray *viewcontrollers =
[Pagecontroller setviewcontrollers:viewcontrollers
Direction:uipageviewcontrollernavigationdirectionforward
Animated:no
Completion:nil];
[Self addchildviewcontroller:pagecontroller];
[Self view] addsubview:[pagecontroller view];

The entire code ends here. Before compiling and running the program, let's analyze the code in the Viewdidload method.

After building the data model, we created a Nsdictionary object. This nsdictionary contains an options for the initialization of the page Controrller object. Here, this option specifies that the spine is located on the left side of the screen (spine is the spine, page binding position, and the book is browsed from the other side):

In the next sentence, instantiate the Uipageviewcontroller with the previous options:

To make the class A data source for the page controller, there are some configurations to make. For example, to make the page full-screen, we need to set its frame:

Before we can show on page 1th, we first need to create a view controller. This can call Viewcontrolleratindex: a convenient method. After acquiring a contentview controller, put it into an array object:

Contentviewcontroller *initialviewcontroller =
Nsarray *viewcontrollers =

Note that only one content view controller is required. Because the page controller is set to display only 1 pages at a time (single-sided). If you configure Pagecontroller to be 2 pages (spine in the center) or double-sided, you need to create 2 content view controllers and put them in the array.

Then, assign the array object to the view controller and set the navigation direction to forward mode:

[Pagecontroller setviewcontrollers:viewcontrollers
Direction:uipageviewcontrollernavigationdirectionforward
Animated:no

Finally, add the page Vview controller to the current view:

[Self addchildviewcontroller:pagecontroller];
[Self view] addsubview:[pagecontroller view];

Running Uipageviewcontroller applications

Run the program by clicking Run. The 1th page will show up. Swipe the screen from right to left, turn to the next page, and swipe in the opposite direction to turn to the previous page.

IOS 5: An example of a Uipageviewcontroller program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.