UIPageViewController 提供了我們方便翻頁的功能,讓我們不用自己製作翻頁的頁面.不過我們還是得提供翻頁的資料給 UIPageViewController 讓他翻頁的時候有 UIViewController 可以顯示.
所以我們必須寫一個類別,實作 <UIPageViewControllerDataSource>這個 Protocol
然後就可以把往前翻頁與往後翻頁的程式碼寫在當中了
程式碼如下:
#import "ModelController.h"
@implementation ModelController
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
DetailViewController *ori = (DetailViewController *) viewController;
DetailViewController *detail= [viewController.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
detail.page = ori.page + 1;
return detail;
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
DetailViewController *ori = (DetailViewController *) viewController;
DetailViewController *detail= [viewController.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
detail.page = ori.page - 1;
return detail;
}
@end