实际上我正在尝试使用PageViewController填充图库。我成功地在视图中添加了滚动图像但是,我在页面View Controller中使用了Next和Previous按钮,但是我使用了这行代码。
- (IBAction)previousButtonPressed:(id)sender {
PageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
- (IBAction)nextButtonPressed:(id)sender {
PageContentViewController *startingViewController = [self viewControllerAtIndex:+1];
NSArray *viewControllers = @[startingViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
因为我在 - (IBAction)nextButtonPressed:(id)发送者使用了viewControllerAtIndex:+1我只是在第一次点击按钮动作时才使按钮动作正常工作。提前致谢
检查此代码: -
- (void)changePage:(UIPageViewControllerNavigationDirection)direction {
NSUInteger pageIndex = ((FooViewController *) [_pageViewController.viewControllers objectAtIndex:0]).pageIndex;
if (direction == UIPageViewControllerNavigationDirectionForward){
pageIndex++;
}else {
pageIndex--;
}
YourViewController *viewController = [self viewControllerAtIndex:pageIndex];
if (viewController == nil) {
return;
}
[_pageViewController setViewControllers:@[viewController] direction:direction animated:YES completion:nil];
}
//上一个按钮
- (IBAction)previousButtonPressed:(id)sender {
[self changePage:UIPageViewControllerNavigationDirectionReverse];
}
//下一个按钮
- (IBAction)nextButtonPressed:(id)sender {
[self changePage:UIPageViewControllerNavigationDirectionForward];
}
Chandan的答案非常有效。在swift 3.0中查看此内容
func changePage(direction:UIPageViewControllerNavigationDirection)
{
var pageIndex = (pageViewController.viewControllers?[0] as! SliderContentVC).pageIndex
if(direction == UIPageViewControllerNavigationDirection.forward)
{
pageIndex = pageIndex! + 1
}
else
{
pageIndex = pageIndex! - 1
}
let viewController = self.viewControllerAtIndex(index: pageIndex!)
if(viewController == nil)
{
return;
}
else
{
self.pageViewController.setViewControllers([viewController!], direction: direction, animated: true, completion: nil)
}
}
//上一个点击
@IBAction func btnGoBack_Click(_ sender: Any)
{
self.changePage(direction: .reverse)
}
//返回点击
@IBAction func btn_GoForward_Click(_ sender: Any)
{
self.changePage(direction: .forward)
}
// viewControllerAtIndex方法
func viewControllerAtIndex(index : Int) -> UIViewController?
{
if(index < 0 || index >= MAX_COUNT)
{
return nil
}
let pageContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "SliderContentVC") as! SliderContentVC
pageContentViewController.pageIndex = index
return pageContentViewController
}