forked from pro648/BasicDemos-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.m
More file actions
99 lines (78 loc) · 2.97 KB
/
Copy pathModel.m
File metadata and controls
99 lines (78 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// Model.m
// PageViewController
//
// Created by pro648 on 2018/6/2.
// Copyright © 2018 pro648. All rights reserved.
//
#import "Model.h"
#import "DataViewController.h"
@interface Model ()
@property (nonatomic, strong, readonly) NSArray *days;
@property (nonatomic, strong, readonly) NSArray *quotes;
@end
@implementation Model
- (instancetype)init {
self = [super init];
if (self) {
// 创建数据模型。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
_days = [[dateFormatter weekdaySymbols] copy];
_quotes = @[@"Everyone who isn’t us is an enemy.",
@"When you play the game of thrones you win or you die. There is no middle ground.",
@"You’re a clever man. But you’re not half as clever as you think you are.",
@"Nobody cares what your father once told you.",
@"Everywhere in the world, they hurt little girls.",
@"I choose violence.",
@"What is Dead May Never Die"
];
}
return self;
}
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index {
// 返回指定index的视图控制器。
if (self.days.count == 0 || index >= self.days.count) {
return nil;
}
DataViewController *dataVC = [[DataViewController alloc] init];
dataVC.itemIndex = index;
dataVC.day = self.days[index];
dataVC.quote = self.quotes[index];
return dataVC;
}
#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
// 返回上一个视图控制器。
NSUInteger index = ((DataViewController *)viewController).itemIndex;
if (index == 0 || index == NSNotFound) {
return nil;
}
--index;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
// 返回下一个视图控制器。
NSUInteger index = ((DataViewController *)viewController).itemIndex;
if (index == self.days.count - 1 || index == NSNotFound) {
return nil;
}
++index;
return [self viewControllerAtIndex:index];
}
// 初始页。
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
NSUInteger currentPage = 2;
if (currentPage >= self.days.count) {
currentPage = 0;
}
DataViewController *dataVC = (DataViewController *)pageViewController.viewControllers.firstObject;
dataVC.itemIndex = currentPage;
dataVC.day = self.days[currentPage];
dataVC.quote = self.quotes[currentPage];
return currentPage;
}
// 共多少页。
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
return self.days.count;
}
@end