OStack程序员社区-中国程序员成长平台

标题: ios - UIPageViewController 具有以编程方式生成的 View 数组 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:01
标题: ios - UIPageViewController 具有以编程方式生成的 View 数组

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

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

@interface PVCPagesViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

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


@end

.

PVCPagesViewController.m
#import "VCPagesViewController.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 actionselector(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 *)pageViewControllerUIPageViewController *)pageViewController viewControllerAfterViewControllerUIViewController *)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 *) pageViewControllerUIPageViewController *)pageViewController viewControllerBeforeViewControllerUIViewController *)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)changePageid)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 "VCPagesViewController.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 "VCContentViewController.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)prepareForSegueUIStoryboardSegue *)segue senderid)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

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



Best Answer-推荐答案


你试过直接用 self.pages 数组设置 pageController 吗?

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

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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4