UIPageViewController以编程方式滑动

时间:2016-04-11 02:46:50

标签: ios objective-c uiviewcontroller

我无法找到解决此问题的此问题的解决方案。我想调用两个代码块,以编程方式刷我的页面视图控制器。

例如[self viewControllerAfterViewController:self]会调用最后一段代码。

viewDidLoad

[super viewDidLoad];
indexvalue = 1;
NSLog(@"main intitated");

// Do any additional setup after loading the view, typically from a nib.
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;

ViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
Request *pvc;
viewcontrollers =[NSArray arrayWithObjects:tvc, pvc, nil];

[self.pageViewController setViewControllers:viewcontrollers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

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

[self.pageViewController didMoveToParentViewController:self];

在滑动手势调用的屏幕之间滑动的代码

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSLog([NSString stringWithFormat:@"%i", indexvalue]);

    if (indexvalue == 0 & check0 < 1) {
        check0 = 1;
        ViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
        indexvalue = 1;
        return tvc;
    }
    else {
        return nil;
    }
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSLog([NSString stringWithFormat:@"%i", indexvalue]);

    if (indexvalue == 1 & check1 < 1) {
        check1 = 1;
        Request *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"Friends"];
        indexvalue = 0 ;
        return  pvc;
    }
    else {
        return nil;
    }
}

1 个答案:

答案 0 :(得分:0)

viewcontrollers =[NSArray arrayWithObjects:tvc, pvc, nil];

replace with 

viewControllers = @[tvc];
viewControllers= [self viewControllerAtIndex:0];

现在添加此方法

- (UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    if (([self.pageTitles count] == 0) || (index >= [self.pageTitles count])) {
        return nil;
    }

    if (index == 0) {
   //first view
    ViewController *pageContentViewController2 = [self.storyboard  instantiateViewControllerWithIdentifier:@"ViewController"];
    return pageContentViewController2;

    }
    //second view

    Request *pageContentViewController3 = [self.storyboard instantiateViewControllerWithIdentifier:@"Request"];
    return pageContentViewController3;
}