我正在尝试根据另一个返回其正在显示的对象的索引的元素创建一个将移动到某个ViewController
的视图。
我能够显示一次特定的ViewController,但是当我从它返回时,无论屏幕上有哪个索引,我都会得到相同的ViewController
。
这是我的代码:
在ViewDidLoad
我设置PageViewController
并将iCarousel添加为子视图。
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil];
self.pageViewController.dataSource = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];
_controller1 = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
_controller2 = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
_controller3 = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
self.viewControllers = [NSArray arrayWithObjects:_controller1, nil];
[self.pageViewController setViewControllers:self.viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
[self.view addSubview:_carousel];
以下是控制用户拉出视图时屏幕上应显示ViewController
的代码。
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (self.pageViewController.viewControllers[0] == self.controller2)
return self.controller1;
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (self.pageViewController.viewControllers[0] == self.controller1) {
if ([self.carousel currentItemIndex] == 2) {
NSLog(@"Giving view %d", [self.carousel currentItemIndex]);
return self.controller2;
} else if ([self.carousel currentItemIndex] == 3) {
NSLog(@"Giving view %d", [self.carousel currentItemIndex]);
return self.controller3;
}
}
return nil;
}
我是否必须以某种方式销毁被提取的ViewController
,或者UIPageViewController
是否可能这样做?
感谢。
修改 当我转换到它们时,试图实例化ViewControllers:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if ([self.carousel currentItemIndex] == 2) {
NSLog(@"load first vc %d", [self.carousel currentItemIndex]);
FirstViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
return secondVC;
} else if ([self.carousel currentItemIndex] == 3) {
NSLog(@"load third vc 2 %d", [self.carousel currentItemIndex]);
SecondViewController *thirdVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
return thirdVC;
}
return nil;
}
使用这种方法,我看到它的日志消息正常工作,但右View
仍然只在第一次加载。我第一次转换到SecondViewController
或ThirdViewController
时,我会收到两次日志消息。
编辑:注意到这一点,我可以无限滚动,因为它会一次又一次地加载ViewController
。因此,如果我将iCarousel
滚动到索引2并向上拉出View
,我会先获得前一个视图,但如果再次“拉”,我会得到正确的视图。
我觉得我非常接近但是我错过了什么小事?
最终编辑:
显然我的代码从一开始就是正确的。以下是some and some more关于UIPageViewControllerTransitionStyleScroll
中的错误的讨论。因此,简单地将转换转换为curl就是我想要做的事情。
如果有人能够回答我将如何以及在何处实施这个the suggested answer那么棒的话。
答案 0 :(得分:0)
用于将UIPageView更改为特定的方法是:
- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion;
要设置特定的viewController,您应该创建一个封装方法的方法,并在索引处获取viewController。为此,您应将视图控制器放在一个数组中,以便可以使用此方法引用它们。
-(void)setView:(NSUInteger)index {
// create a view controller
UIViewController *startingViewController = (UIViewController *)[self.arrayOfViewControllers objectAtIndex:index];
// place it in an array
NSArray *viewControllers = @[startingViewController];
// Above method
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}