跳转到UIPageViewController中的perticular viewcontroller

时间:2015-01-19 04:45:22

标签: ios objective-c iphone

我的App中有15个viewcontrollers。我想横向滑动它们。 我在uipageviewcontroller中添加了所有这些视图控制器(我在第一个viewcontroller中编写了这段代码)。 15个视图控制器中的每一个都包含要转到其余所有viewcontrolers.so的按钮。我怎样才能跳转到pageviewcontroller里面的这些viewcontrollers?,这样我就无法进入水平滑动之外。

这是我的代码。目前我在uipageviewcontroller中添加了三个viewcontrollers。

这是我在Appdelegate中所写的内容。

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, strong) UIPageViewController *pageViewController;

@property (nonatomic, strong) NSArray *contentViewControllers;

这是我的viewcontroller.m(即初始viewcontroller)。

APP_DELEGATE.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageView"];
    APP_DELEGATE.pageViewController.dataSource = self;

         UIViewController *init = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];

        FirstViewController *first = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];

        secondViewController *second = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];

        thirdViewController *third = [self.storyboard instantiateViewControllerWithIdentifier:@"thirdViewController"];


        APP_DELEGATE.contentViewControllers = [NSArray arrayWithObjects:init,first,second,third,first,nil];

        [APP_DELEGATE.pageViewController setViewControllers:@[first] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];

        [self addChildViewController:APP_DELEGATE.pageViewController];
        [self.view addSubview:APP_DELEGATE.pageViewController.view];
        [APP_DELEGATE.pageViewController didMoveToParentViewController:self];

        self.view.backgroundColor = [UIColor colorWithRed:(248.0/255.0) green:(248.0/255.0) blue:(248.0/255.0) alpha:1.0];
    }

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
          viewControllerBeforeViewController:(UIViewController *)viewController {

        NSUInteger index = [APP_DELEGATE.contentViewControllers indexOfObject:viewController];

        if (index == 1) {
            return nil;
        }

        return APP_DELEGATE.contentViewControllers[index - 1];
    }

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
           viewControllerAfterViewController:(UIViewController *)viewController {

        NSUInteger index = [APP_DELEGATE.contentViewControllers indexOfObject:viewController];

        if (index >= APP_DELEGATE.contentViewControllers.count - 1) {
            return nil;
        }

        return APP_DELEGATE.contentViewControllers[index + 1];
    }


    This is my secondViewcontroller.m

    - (IBAction)moveToFirstButtonAction:(id)sender {

        FirstViewController *fvc = [APP_DELEGATE.contentViewControllers objectAtIndex:1];

        fvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:fvc animated:YES completion:nil];

    }

使用此方法,我跳转到firstViewcontroller。但是我从水平滑动中走出来。即水平滑动不起作用。

1 个答案:

答案 0 :(得分:0)

您可以在数组中获取viewcontrollers的引用。像这样:

PageViewController *pageView;

将是您的页面视图。

@property (nonatomic, readonly) NSArray *viewControllers;

NSArray *viewControlers = pageView.viewControllers;

然后你可以转到任何一个viewcontroller,只需传递你想去的viewcontroller的索引。

非常感谢。

此致 哈利