所以我到处找,似乎找不到我要找的东西。基本上,我的应用程序中有 3 个不同的 View Controller 。 (MainViewController, ChildViewController, TwitterViewController) 起初我练习只使用 ChildViewController 并更新一个标签来告诉你你在哪个页面。但是,我想将 TwitterViewController 添加为“第 2 页”并将 ChildViewController 保留为“第 1 页”。

目前,我将 MainViewController 设置为 Delegate 和 DataSource,并将代码放回仅使用一个“ChildViewController”作为不同页面。请让我知道如果这不使。如果需要,我会尝试重申。下面是我的代码。

MainViewController.m

#import "MainViewController.h"
#import "ChildViewController.h"
#import "TwitterViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];

    ChildViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];

    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    pageControl.currentPageIndicatorTintColor = [UIColor redColor];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [(ChildViewController *)viewController index];

    if (index == 0) {
        return nil;
    }

    index--;

    return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [(ChildViewController *)viewController index];

    index++;

    if (index == 2) {
        return nil;
    }

    return [self viewControllerAtIndex:index];
}

- (ChildViewController *)viewControllerAtIndex:(NSUInteger)index {
    ChildViewController *first = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
    first.index = index;

    return first;
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return 2;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

ChildViewController.m
#import "ChildViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ChildViewController ()

@end

@implementation ChildViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.screenNumber.text = [NSString stringWithFormat:@"This is Page #%d", self.index];
    self.screenNumber.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1];
    self.screenNumber.layer.cornerRadius =10;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

TwitterViewController 是默认的,还没有被编写。目前只是用一个空白的 ViewController 尝试它。

任何帮助都会很棒!!谢谢你们!

最佳答案

您将需要每个 View Controller 的索引。因此,如果您创建一个 UIViewController 类作为基类,该类将具有一个属性“index”来存储索引,那就太好了。然后子类化这个基类来创建 ChildViewController 和 TwitterViewContoller。将方法 (ChildViewController *)viewControllerAtIndex:(NSUInteger)index 更改为(BaseViewController *)viewControllerAtIndex:(NSUInteger)index

在方法内部,根据索引返回适当的 View Controller 。

关于ios - 在 UIPageViewController 中使用 2 个不同的 View Controller 作为页面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20601707/

10-10 20:33