• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:01:39 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

所以我需要创建一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap