以下は ModelController.m のコードです。
- (id)init
{
self = [super init];
if (self) {
// Create the data model.
DataHandler *handler = [DataHandler defaultDataHandler];
[handler syncData];
NSInteger tempTotal=1;//[[DataHandler defaultDataHandler] getTotalContents];
NSMutableDictionary *favs=[Settings getSetting:kFavList];
if(favs!=nil){
tempTotal=[favs count]+1;
}
NSInteger TotalItems=tempTotal;//tempTotal>=6?tempTotal:6;
NSInteger PageSize=6;
NSInteger PageNo= (TotalItems/PageSize);
if(TotalItems%PageSize!=0){
PageNo++;
}
self.TotalPage=PageNo;
NSInteger i=0,temp=PageSize;
self.pageArray=[[NSMutableDictionary alloc] initWithCapacity:PageNo];
for (i=0; i<PageNo; i++) {
temp=(PageNo==i+1 && TotalItems%PageSize!=0)? TotalItems%PageSize:PageSize;
[self.pageArray setObject:[NSString stringWithFormat:@"%d",temp] forKey:[NSString stringWithFormat:@"%d",i+1]];
}
//NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//_pageData = [[dateFormatter monthSymbols] copy];
self.pageData=[self.pageArray allKeys];
}
return self;
}
- (HomeViewController *)viewControllerAtIndex:(NSUInteger)index {
NSLog(@"viewControllerAtIndex is called..");
// Return the data view controller for the given index.
if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
return nil;
}
// Create a new view controller and pass suitable data.
HomeViewController *homeViewController = nil;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
homeViewController = [[[HomeViewController alloc] initWithNibName:@"HomeViewController_iPhone" bundle:nil] autorelease];
} else {
homeViewController = [[[HomeViewController alloc] initWithNibName:@"HomeViewController_iPad" bundle:nil] autorelease];
}
homeViewController.dataObject = [self.pageData objectAtIndex:index];
homeViewController.PageItemsCount= [[self.pageArray valueForKey:[NSString stringWithFormat:@"%d",index+1]] integerValue];
homeViewController.TotalPage=self.TotalPage;
homeViewController.CurrentPage=index+1;
return homeViewController;
}
- (NSUInteger)indexOfViewController:(HomeViewController *)viewController
{
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return viewController.CurrentPage-1;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(HomeViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(HomeViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
これは画面をスワイプすると正常に動作しますが、コードが非常に長いため、メイン ビュー ページ (この場合は homeViewController.m) の任意のボタン タップで画面を変更したいので、ここにコードを投稿しませんでした。
誰かがこれを達成する方法を教えてもらえますか?
ありがとうアシッシュ