所以我到处寻找,似乎无法找到我正在寻找的东西。基本上,我的应用程序中有3个不同的视图控制器。 (MainViewController,ChildViewController,TwitterViewController)起初我只是练习使用ChildViewController并更新标签来告诉你在哪个页面。但是,我想在TwitterViewController中添加“Page 2”并将ChildViewController保持为“Page 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。
任何帮助都会很棒!!谢谢你们!
答案 0 :(得分:4)
每个视图控制器都需要索引。因此,如果您将UIViewController类创建为基类,它将具有存储索引的属性“index”,那将是一件好事。然后继承此基类以创建ChildViewController和TwitterViewContoller。将方法(ChildViewController *)viewControllerAtIndex:(NSUInteger)index
更改为
(BaseViewController *)viewControllerAtIndex:(NSUInteger)index
。
在方法内部,根据索引返回适当的视图控制器。