Im trying to make a UIPageViewController with a UITableViewController within.
Im having trouble getting the UIPageViewController to work properly. ViewDidLoad keeps getting called and the view never shows on my iOS Simulator.
Here are my classes:
ProgramViewController.h:
#import <UIKit/UIKit.h>
#import "TableViewController.h"
@interface ProgramViewController : UIPageViewController <UIPageViewControllerDataSource>
{
UIPageViewController *programViewController;
NSArray *pageContent;
}
@property (strong, nonatomic) UIPageViewController *programViewController;
@property (strong, nonatomic) NSArray *pageContent;
@property (strong, nonatomic) NSArray *listArray;
@property (strong, nonatomic) NSArray *pageTitles;
ProgramViewController.m
#import "ProgramViewController.h"
@implementation ProgramViewController
@synthesize programViewController,pageContent;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"ViewDidLoad");
_pageTitles = @[@"page0", @"page1", @"page2"];
self.programViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"2"];
self.programViewController.dataSource = self;
TableViewController *startingViewController = [self viewControllerAtIndex:0];
NSLog(@"description = %@",[startingViewController description]);
NSArray *viewControllers = @[startingViewController];
[self.programViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
// Change the size of page view controller
self.programViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:programViewController];
[self.view addSubview:programViewController.view];
[self.programViewController didMoveToParentViewController:self];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((TableViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((TableViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
return nil;
}
return [self viewControllerAtIndex:index];
}
- (TableViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
return nil;
}
NSLog(@"description = %d",[self.pageTitles count]);
// Create a new view controller and pass suitable data.
TableViewController *tableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"2.0"];
tableViewController.titleText = self.pageTitles[index];
tableViewController.pageIndex = index;
return tableViewController;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.pageTitles count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
And then i have the UITableViewController class. This class works perfectly when i use it by itself, but i dont know if is a problem of trying to put a UITableViewController inside a UIPageViewController:
TableViewController.h
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property NSUInteger pageIndex;
@property NSString *titleText;
@end
TableViewController.m
#import "TableViewController.h"
#import "ProgramCell.h"
@implementation TableViewController
{
NSArray *arrayDays;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
for(int i=0;i<10;i++){
}
arrayDays = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"description = %d",[arrayDays count]);
return [arrayDays count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustomCell";
ProgramCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
NSLog(@"description = %@",[cell description]);
if (cell == nil) {
NSLog(@"cell nil");
cell = [[ProgramCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.durationLabelCell.text = @"HOLA";
return cell;
}
@end
On the Xcode console, i keep getting the result of the "NSLog(@'ViewDidLoad');" on my ProgramViewController.h. The app instantiates the ViewController as long as it has memory and then it crashes.
If someone has any idea why this is happening it would be great. Thanks
IMP: Your ProgramViewController
class has a variable (and property) named programViewController
so the following explanation may sound confusing. To make it easier, I will mention the class
in bold : ProgramViewController
(with a capital P) and the variable in normal font : programViewController
(with small p).
This line of code in your viewDidLoad
:
self.programViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"2"];
instantiates a new page view controller from storyboard and which you later add to the ProgramViewController
.
If the view controller with identifier 2 in your storyboard is also a ProgramViewController
(which I suspect is the case), you are creating a recurrence. Every time a ProgramViewController
is added and in turn creates a new one in viewDidLoad
.
You do not need another programViewController
variable within your ProgramViewController
class. Delete that variable and the property. Call setViewControllers
on with self
instead of self.programViewController
and delete all the lines where you add the programViewController
as a child view and controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.