gpt4 book ai didi

iOS - UIPageViewControllerDataSource 方法调用了两次

转载 作者:可可西里 更新时间:2023-11-01 03:09:00 25 4
gpt4 key购买 nike

我发现这两个UIPageViewControllerDataSource方法有一个奇怪的问题:第一次进入page view controller场景,在controller的内容上拖动,不管拖动的方向是什么,都调用数据源方法。我想当我在第一页并向右拖动时,这意味着第一页之前没有更多页面,这两个方法都不应该被调用。如果我向左拖动,则只应调用 after 方法。

我关注了this post设置 View Controller (除了我在 Storyboard上没有单独的页面 View Controller 。我使用传统的 [UIPAgeViewController alloc] init] 方法来实例化 Controller )。下面是我的代码:

对于实际显示页面 View Controller 内容的 View Controller (只显示viewDidLoad的相关部分):

 - (void)viewDidLoad 
{
// initialize page view controller
_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

// set up the initial scene of the page view controller
UIViewController *viewController = [self viewControllerAtIndex:0];
[self.pageViewController setViewControllers:@[viewController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];

// adjust the size of the page view controller, -44 to show the buttons at the bottom
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44);

// display the content of the page view controller
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PWCPageContentViewController *)viewController).index;
if (index == 0 || index == NSNotFound) {
return nil;
}
--index;
return [self viewControllerAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PWCPageContentViewController *)viewController).index;
if (index == self.numberOfPages - 1 || index == NSNotFound) {
return nil;
}
++index;
return [self viewControllerAtIndex:index];
}

- (PWCPageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
PWCPageContentViewController *viewController =
[self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
viewController.index = index;
NSString *text = [self.notes getNoteAtIndex:index];
[viewController.textView setText:text];

return viewController;
}

对于 PWCPageContentViewController.h:

#import <UIKit/UIKit.h>

@class PWCNotes;

@interface PWCPageContentViewController : UIViewController <UITextViewDelegate>

@property NSUInteger index;
@property (weak, nonatomic) IBOutlet UITextView *textView;

- (void)dismissKeyboard;

@end

对于 PWCPageContentViewController.m:

#import "PWCPageContentViewController.h"

@interface PWCPageContentViewController ()

@end

@implementation PWCPageContentViewController

- (void)viewDidLoad
{
[super viewDidLoad];

self.textView.delegate = self;
}

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

- (void)dismissKeyboard
{
if ([self.textView isFirstResponder]) {
// dismiss the keyboard when hitting save
[self.textView resignFirstResponder];
}
}

@end

这是一个已知问题还是我遗漏了什么?

最佳答案

Swift 中这个问题的解决方案:

我在使用超过 3 个 UIPageViewController 时遇到了错误(例如尝试使用 4 个 UIPageViewController,使用@Rashad 的代码片段)。问题来自缺少且未实现的可选 UIPageViewControllerDelegate 方法,这导致 UIPageControl 变得疯狂和不稳定。

请在下面找到修复错误的具体方法:

/*
Optional UIPageViewControllerDelegate method

Sent when a gesture-initiated transition ends. The 'finished' parameter indicates whether
the animation finished, while the 'completed' parameter indicates whether the transition completed or bailed out (if the user let go early).
*/
func pageViewController(pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [AnyObject],
transitionCompleted completed: Bool)
{
// Turn is either finished or aborted
if (completed && finished) {
let currentDisplayedViewController = self.pageViewController!.viewControllers[0] as! ContentViewController
self.pageControl.currentPage = currentDisplayedViewController.index
}
}

如果您想获得完整的工作项目,请在下面找到 Github 链接:

( Github link of the project )

关于iOS - UIPageViewControllerDataSource 方法调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926226/

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