Я обнаружил странную проблему с двумя методами UIPageViewControllerDataSource
: когда я впервые вхожу в сцену контроллера представления страницы и перетаскиваю содержимое контроллера, независимо от направления перетаскивания, вызываются оба метода источника данных. Я полагаю, что когда я нахожусь на первой странице и перетаскиваю вправо, что означает, что перед первой страницей больше нет страницы, ни один из методов не должен вызываться. И если я перетащу влево, должен быть вызван только метод after.
Я следил за этой публикацией, чтобы настроить контроллеры представления (за исключением того, что у меня нет отдельного контроллера представления страницы на раскадровке. Я использовал традиционный метод [UIPAgeViewController alloc] init]
для создания экземпляра контроллера). Ниже мой код:
Для контроллера представления, который фактически отображает содержимое контроллера представления страницы (показана только соответствующая часть 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
Так это известная проблема или я что-то упускаю?