ViewDidLoad不断在UIPageViewController上调用

时间:2015-07-01 09:45:08

标签: ios objective-c uipageviewcontroller

我试图用UITableViewController制作一个UIPageViewController。

我无法让UIPageViewController正常工作。 ViewDidLoad不断被调用,视图永远不会显示在我的iOS模拟器上。

以下是我的课程:

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;
}

然后我有了UITableViewController类。当我单独使用它时,这个类工作得很好,但我不知道是否是试图将UITableViewController放在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

在Xcode控制台上,我不断获得&#34; NSLog的结果(@&#39; ViewDidLoad&#39;);&#34;在我的ProgramViewController.h上。只要它有内存然后崩溃,app就会实例化ViewController。

如果有人知道为什么会这样,那就太好了。感谢

1 个答案:

答案 0 :(得分:0)

IMP:您的 ProgramViewController 类有一个名为programViewController的变量(和属性),因此以下说明可能会让您感到困惑。为方便起见,我将以粗体显示class ProgramViewController (使用大写字母P)和常规字体中的变量:programViewController(小p)的

viewDidLoad中的这行代码:

self.programViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"2"];

从故事板中实例化一个新的页面视图控制器,稍后您将其添加到 ProgramViewController

如果故事板中标识符 2 的视图控制器也是 ProgramViewController (我怀疑是这种情况),则表示您正在创建重复。每次添加 ProgramViewController ,然后在viewDidLoad中创建一个新的。

您的 programViewController 类中不需要另外ProgramViewController个变量。删除该变量和属性。使用setViewControllers而不是self致电self.programViewController,并删除将programViewController添加为子视图和控制器的所有行。