实际上我正在尝试使用PageViewController填充图库。我成功地在视图中添加了滚动图像但是,我在页面View Controller 中使用了下一个和上一个按钮,但是我使用了这行代码。
- (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 我得到的按钮动作仅适用于第一次点击按钮动作。 提前致谢
答案 0 :(得分:12)
检查此代码: -
- (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];
}
答案 1 :(得分:1)
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
}