• <i id='7BwOT'><tr id='7BwOT'><dt id='7BwOT'><q id='7BwOT'><span id='7BwOT'><b id='7BwOT'><form id='7BwOT'><ins id='7BwOT'></ins><ul id='7BwOT'></ul><sub id='7BwOT'></sub></form><legend id='7BwOT'></legend><bdo id='7BwOT'><pre id='7BwOT'><center id='7BwOT'></center></pre></bdo></b><th id='7BwOT'></th></span></q></dt></tr></i><div id='7BwOT'><tfoot id='7BwOT'></tfoot><dl id='7BwOT'><fieldset id='7BwOT'></fieldset></dl></div>

      <legend id='7BwOT'><style id='7BwOT'><dir id='7BwOT'><q id='7BwOT'></q></dir></style></legend>
        <bdo id='7BwOT'></bdo><ul id='7BwOT'></ul>
    1. <small id='7BwOT'></small><noframes id='7BwOT'>

      1. <tfoot id='7BwOT'></tfoot>
      2. PageViewController 委托函数调用了两次

        时间:2023-06-01

            <tbody id='qQz9v'></tbody>
            <legend id='qQz9v'><style id='qQz9v'><dir id='qQz9v'><q id='qQz9v'></q></dir></style></legend>
          • <i id='qQz9v'><tr id='qQz9v'><dt id='qQz9v'><q id='qQz9v'><span id='qQz9v'><b id='qQz9v'><form id='qQz9v'><ins id='qQz9v'></ins><ul id='qQz9v'></ul><sub id='qQz9v'></sub></form><legend id='qQz9v'></legend><bdo id='qQz9v'><pre id='qQz9v'><center id='qQz9v'></center></pre></bdo></b><th id='qQz9v'></th></span></q></dt></tr></i><div id='qQz9v'><tfoot id='qQz9v'></tfoot><dl id='qQz9v'><fieldset id='qQz9v'></fieldset></dl></div>

            <small id='qQz9v'></small><noframes id='qQz9v'>

              <bdo id='qQz9v'></bdo><ul id='qQz9v'></ul>

              1. <tfoot id='qQz9v'></tfoot>
                  本文介绍了PageViewController 委托函数调用了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 UIPageViewController 为我的应用程序制作产品导览.

                  I am working with UIPageViewController , to make a product tour for my application.

                  我点击了这个链接 http://www.appcoda.com/uipageviewcontroller-tutorial-intro/

                  我正在做的是根据我得到的索引值在滑动时更改 root VC" 的背景颜色的简单任务,但是由于委托函数被调用两次,我的索引值是不正确,因此,我无法正确处理,下面是我的代码

                  I am doing is simple task of changing backgound color of my "root VC" on swipe, based on the index value I get, but as the delegate functions are called twice, my index value is not correct and because of that, I am not able to get it right, below is my code

                  #import "APPViewController.h"
                  #import "APPChildViewController.h"
                  
                  @interface APPViewController ()
                  
                  @end
                  
                  @implementation APPViewController
                  
                  - (void)viewDidLoad {
                  
                      [super viewDidLoad];
                  
                      self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
                  
                      self.pageController.dataSource = self;
                      [[self.pageController view] setFrame:CGRectMake(0, 0, 320, 500)];
                  
                      APPChildViewController *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];
                  
                  }
                  
                  - (void)didReceiveMemoryWarning {
                  
                      [super didReceiveMemoryWarning];
                      // Dispose of any resources that can be recreated.
                  
                  }
                  
                  - (APPChildViewController *)viewControllerAtIndex:(NSUInteger)index {
                  
                      APPChildViewController *childViewController = [[APPChildViewController alloc] initWithNibName:@"APPChildViewController" bundle:nil];
                      childViewController.index = index;
                      childViewController.view.backgroundColor = [UIColor clearColor];
                  
                      if(index == 0)
                      {
                            self.view.backgroundColor = [UIColor redColor];
                       }
                  
                      if(index == 1)
                      {
                            self.view.backgroundColor = [UIColor blueColor];
                       }
                  
                      if(index == 2)
                      {
                            self.view.backgroundColor = [UIColor greenColor];
                       }
                  
                  
                      return childViewController;
                  
                  }
                  
                  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
                  
                      NSUInteger index = [(APPChildViewController *)viewController index];
                  
                      if (index == 0) {
                          return nil;
                      }
                  
                      // Decrease the index by 1 to return
                      index--;
                  
                     return [self viewControllerAtIndex:index];
                  
                  }
                  
                  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
                  
                      NSUInteger index = [(APPChildViewController *)viewController index];
                  
                      index++;
                  
                      if (index == 3) {
                          return nil;
                      }
                  
                     return [self viewControllerAtIndex:index];
                  
                  }
                  
                  - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
                      // The number of items reflected in the page indicator.
                      return 3;
                  }
                  
                  - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
                      // The selected item reflected in the page indicator.
                      return 0;
                  }
                  

                  请帮帮我,我不知道哪里出错了

                  Please help me out, I am not getting where I am going wrong

                  问候兰吉特

                  推荐答案

                  找了很多.

                  我收到了:

                  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
                  - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
                  

                  2 个函数用于获取当前 pageViewController 后面或前面的 pageViewController.

                  2 functions use to get pageViewController behind or in front of current pageViewController.

                  我认为获取当前 pageViewController 很困难

                  I thinks it's difficult to get current pageViewController

                  我的建议:

                  在 UIPageViewControllerDelegate 中,它有一个功能:

                  In UIPageViewControllerDelegate, it have a function :

                   - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers;
                  

                  这个函数给你一个pendingViewControllers 数组和当前pageViewController 数组.所以你可以这样实现:

                  This function to give you a pendingViewControllers array and this's current pageViewController array. So you can implement like that :

                  - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
                  {
                  
                  
                  if([pendingViewControllers count]>0)
                    {
                       NSUInteger index =[(APPChildViewController*)[pendingViewControllers objectAtIndex:0] index];
                  
                      if(index == 0)
                      {
                          self.view.backgroundColor = [UIColor redColor];
                      }
                  
                      if(index == 1)
                      {
                          self.view.backgroundColor = [UIColor blueColor];
                      }
                  
                      if(index == 2)
                      {
                          self.view.backgroundColor = [UIColor greenColor];
                      }
                  
                  
                    }
                  }
                  

                  在 viewDidLoad 中,添加:

                  In viewDidLoad, you add :

                      self.pageController.delegate = self;
                  
                      self.view.backgroundColor = [UIColor redColor]; //set first background.
                  

                  在 'APPViewController.h' 中你确定添加:

                  In 'APPViewController.h' you sure add:

                  @interface APPViewController : UIViewController<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
                  

                  记住:删除这段代码(在'viewControllerAtIndex'函数中)

                  Remember : remove this code (in 'viewControllerAtIndex' function)

                  if(index == 1)
                  {
                      self.view.backgroundColor = [UIColor redColor];
                  }
                  
                  if(index == 2)
                  {
                      self.view.backgroundColor = [UIColor blueColor];
                  }
                  
                  if(index == 3)
                  {
                      self.view.backgroundColor = [UIColor greenColor];
                  }
                  

                  如果您有任何问题,请告诉我.

                  Let's me know if you have any questions.

                  这篇关于PageViewController 委托函数调用了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我们如何以编程方式改变 iPhone 屏幕的亮度? 下一篇:如何在 iOS 5.1 中以 MM-dd-yy 格式显示选取器视图

                  相关文章

                  最新文章

                  <tfoot id='Cld3g'></tfoot>

                  1. <i id='Cld3g'><tr id='Cld3g'><dt id='Cld3g'><q id='Cld3g'><span id='Cld3g'><b id='Cld3g'><form id='Cld3g'><ins id='Cld3g'></ins><ul id='Cld3g'></ul><sub id='Cld3g'></sub></form><legend id='Cld3g'></legend><bdo id='Cld3g'><pre id='Cld3g'><center id='Cld3g'></center></pre></bdo></b><th id='Cld3g'></th></span></q></dt></tr></i><div id='Cld3g'><tfoot id='Cld3g'></tfoot><dl id='Cld3g'><fieldset id='Cld3g'></fieldset></dl></div>

                    <small id='Cld3g'></small><noframes id='Cld3g'>

                      <bdo id='Cld3g'></bdo><ul id='Cld3g'></ul>
                    <legend id='Cld3g'><style id='Cld3g'><dir id='Cld3g'><q id='Cld3g'></q></dir></style></legend>