gpt4 book ai didi

ios - 在 UIPageViewController 中使用 2 个不同的 View Controller 作为页面

转载 作者:可可西里 更新时间:2023-11-01 05:41:15 25 4
gpt4 key购买 nike

所以我找遍了所有地方,似乎找不到我要找的东西。基本上,我的应用程序中有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com