菜鸟教程小白 发表于 2022-12-12 11:01:39

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


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

<pre><code>PVCPagesViewController.h
#import &lt;UIKit/UIKit.h&gt;
#import &#34;PVCContentViewController.h&#34;

@interface PVCPagesViewController : UIViewController &lt;UIPageViewControllerDataSource, UIPageViewControllerDelegate&gt;

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


@end
</code></pre>

<p>.</p>

<pre><code>PVCPagesViewController.m
#import &#34;PVCPagesViewController.h&#34;

@interface PVCPagesViewController () {
    NSArray *pages;
}

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


@end

@implementation PVCPagesViewController


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

    //I think my problem is around here?

    PVCContentViewController *page1 = [ init];
    ;
    PVCContentViewController *page2 = [ init];
    ;
    PVCContentViewController *page3 = [ init];
    ;
    PVCContentViewController *page4 = [ init];
    ;


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

    self.pageController = [ initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    ;
    ;

    [ setFrame:[ bounds]];
    NSArray *viewControllers = ];
    ;
    ;


    ;

    ;

    ;
    [ addSubview:];
    ;

    ];
}

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

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

    NSUInteger currentIndex = ;    // get the index of the current view controller on display
    ;                   // 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 &lt; -1) {
      return ;                   // return the next view controller
    } else {
      return nil;                                                         // do nothing
    }
}


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

    NSUInteger currentIndex = ;    // get the index of the current view controller on display
    ;                   // 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 &gt; 0) {
      return ;                   // return the previous viewcontroller
    } else {
      return nil;                                                         // do nothing
    }
}

- (void)changePage:(id)sender {

    UIViewController *visibleViewController = self.pageController.viewControllers;
    NSUInteger currentIndex = ;

    NSArray *viewControllers = ];

    if (self.pageControl.currentPage &gt; currentIndex) {
      ;
    } else {
      ;

    }
}

@end
</code></pre>

<p>.</p>

<pre><code>PVCContentViewController.h
#import &lt;UIKit/UIKit.h&gt;
#import &#34;PVCPagesViewController.h&#34;

@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
</code></pre>

<p>最后:</p>

<pre><code>PVCContentViewController.m
#import &#34;PVCContentViewController.h&#34;

@interface PVCContentViewController ()

@end

@implementation PVCContentViewController
@synthesize label3,label2,label1;

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

- (void)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 .
    // Pass the selected object to the new view controller.
}
*/

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

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

@end
</code></pre>

<p>我从这里借用了启动源代码:
<a href="https://github.com/hackin247/UIPageViewController" rel="noreferrer noopener nofollow">https://github.com/hackin247/UIPageViewController</a> </p>

<p>我的源代码:
<a href="https://github.com/4FunAndProfit/UIPageViewControllerHelp" rel="noreferrer noopener nofollow">https://github.com/4FunAndProfit/UIPageViewControllerHelp</a> </p>

<p>如果您需要更多信息,请告诉我!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你试过直接用 <code>self.pages</code> 数组设置 <code>pageController</code> 吗?</p>

<p><code>;</code></p>

<p>我没有看到创建另一个数组(<code>viewControllers</code>)的意义。尝试获取下一页时,这可能会导致问题,因为 <code>pageController</code> 只有一个 <code>viewController</code> (<code></code> ) 但“数据源”说它应该有更多基于 <code>self.pages.count</code>。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIPageViewController 具有以编程方式生成的 View 数组,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/25172721/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/25172721/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIPageViewController 具有以编程方式生成的 View 数组