gpt4 book ai didi

ios - UIPageViewController 具有以编程方式生成的 View 数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:26 27 4
gpt4 key购买 nike

所以我需要创建一个显示几个 View (1-25) 的 UIPageViewController。
基本上我有一个测验应用程序,对于每个错误的问题,我都会将问题编号 (1-25) 保存在 NSMutableArray 中。测验完成后,我想向用户展示哪个测验答案是错误的。我有设置错误答案 View 的方法(与我在测验中设置 View 的方法相同)。这一直给我“Thread1:”错误。

PVCPagesViewController.h
#import <UIKit/UIKit.h>
#import "PVCContentViewController.h"

@interface PVCPagesViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;


@end

.

PVCPagesViewController.m
#import "PVCPagesViewController.h"

@interface PVCPagesViewController () {
NSArray *pages;
}

@property (retain, nonatomic) NSArray *pages;
@property (strong, nonatomic) UIPageViewController *pageController;


@end

@implementation PVCPagesViewController


- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

//I think my problem is around here?

PVCContentViewController *page1 = [[PVCContentViewController alloc] init];
[page1 example1];
PVCContentViewController *page2 = [[PVCContentViewController alloc] init];
[page1 example2];
PVCContentViewController *page3 = [[PVCContentViewController alloc] init];
[page1 example3];
PVCContentViewController *page4 = [[PVCContentViewController alloc] init];
[page1 example2];


// load the view controllers in our pages array
self.pages = [[NSArray alloc] initWithObjects:page1, page2, page3, page4, nil];

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[self.pageController setDelegate:self];
[self.pageController setDataSource:self];

[[self.pageController view] setFrame:[[self view] bounds]];
NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:0]];
[self.pageControl setCurrentPage:0];
[self.pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];


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

[self addChildViewController:self.pageController];

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

[self.view sendSubviewToBack:[self.pageController view]];
}

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

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

NSUInteger currentIndex = [self.pages indexOfObject:viewController]; // get the index of the current view controller on display
[self.pageControl setCurrentPage:self.pageControl.currentPage+1]; // move the pageControl indicator to the next page

// check if we are at the end and decide if we need to present the next viewcontroller
if ( currentIndex < [self.pages count]-1) {
return [self.pages objectAtIndex:currentIndex+1]; // return the next view controller
} else {
return nil; // do nothing
}
}


- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

NSUInteger currentIndex = [self.pages indexOfObject:viewController]; // get the index of the current view controller on display
[self.pageControl setCurrentPage:self.pageControl.currentPage-1]; // move the pageControl indicator to the next page

// check if we are at the beginning and decide if we need to present the previous viewcontroller
if ( currentIndex > 0) {
return [self.pages objectAtIndex:currentIndex-1]; // return the previous viewcontroller
} else {
return nil; // do nothing
}
}

- (void)changePage:(id)sender {

UIViewController *visibleViewController = self.pageController.viewControllers[0];
NSUInteger currentIndex = [self.pages indexOfObject:visibleViewController];

NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:self.pageControl.currentPage]];

if (self.pageControl.currentPage > currentIndex) {
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
} else {
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];

}
}

@end

.

PVCContentViewController.h
#import <UIKit/UIKit.h>
#import "PVCPagesViewController.h"

@interface PVCContentViewController : UIPageViewController

@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UILabel *label3;

-(void)example1;
-(void)example2;
-(void)example3;

@end

最后:

PVCContentViewController.m
#import "PVCContentViewController.h"

@interface PVCContentViewController ()

@end

@implementation PVCContentViewController
@synthesize label3,label2,label1;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

-(void)example1{
label1.text = @"View 1";
label2.text = @"View 1";
label3.text = @"view 1";
}

-(void)example2{
label1.text = @"View 2";
label2.text = @"View 2";
label3.text = @"view 2";
}
-(void)example3{
label1.text = @"View 3";
label2.text = @"View 3";
label3.text = @"view 3";
}

@end

我从这里借用了启动源代码: https://github.com/hackin247/UIPageViewController

我的源代码: https://github.com/4FunAndProfit/UIPageViewControllerHelp

如果您需要更多信息,请告诉我!

最佳答案

您是否尝试过直接使用 self.pages 数组设置 pageController

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

我看不出再创建一个数组 (viewControllers) 有什么意义。尝试获取下一页时,这可能会导致问题,因为 pageController 只有一个 viewController ([self.pages objectAtIndex:0] ) 但“数据源”表示它应该有更多基于 self.pages.count 的内容。

关于ios - UIPageViewController 具有以编程方式生成的 View 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25172721/

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