UIPageViewController与不同的ViewControllers,正确的方法? [英] UIPageViewController with different ViewControllers, right way?

查看:93
本文介绍了UIPageViewController与不同的ViewControllers,正确的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个表视图控制器和一个普通的视图控制器,我想这样做,以便我可以在视图控制器之间滑动,就像应用程序的主屏幕(明显相同类型的视图控制器)。

Basically, I have a Table View Controller and a normal View Controller, I want to make it so that I can swipe between the view controllers like on the home screen of app (obvious same type of view controller).

但我发现的是,当您有多个相同类型<$的多个视图时,会使用 UIpageviewcontroller c $ c> viewcontroller 例如在图片之间滑动,你可以在类中设置一个索引,并在索引中设置一个图像数组,然后按照这种方式进行设置。

But the thing I am finding is that the UIpageviewcontroller is usaully used when you have multiple views of the same type of viewcontroller for example swiping between pictures, you could just set an index in the class and an array of images relating to the index and set it up that way.

但是我试图在这个设置中获得不同的视图控制器。现在它是两个但肯定会达到大约4个。

But I'm trying to get different view controllers in this set up. Right now it's two but will definitely go up to about 4.

UIPageViewController 设置它的正确方法或其他。

Is UIPageViewController the right way to set it up or something else.

编辑:下面添加的代码

    //
//  ViewController.m
//  testb
//
#import "MainPostController.h"
#import "MainTableViewController.h"
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize pageViewController;
@synthesize indexValue = _indexValue;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.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;

    MainTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
    MainPostController *pvc;
    NSArray *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 - 30);

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (self.indexValue == 1) {
        MainPostController *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPostController"];
        self.indexValue = 0 ;
        return  pvc;

    }
    else return nil;




}

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

    if (self.indexValue == 0) {
        MainTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
        self.indexValue = 1;
        return tvc;

    }
    else return nil;


}
@end

我想要初始视图是表视图控制器(tvc),我希望Post视图控制器(pvc)在其左侧。我可以加载它并轻松地在两者之间滑动,但有时我可以进一步滑动边界,所以基本上如果我开始在tvc - > pvc - > tvc然后我可以通过向右滑动到同一个tvc然后进一步到达边界或走另一条路tvc - > pvc - > tvc - > pvc - > pvc通过向左滑动进行最后一次转换。最终达到了界限。一旦应用程序在tvc初始启动我无法前进一个页面,(这应该发生什么)如果我去tvc - > pvc并尝试去左边的页面它不会让我(这是应该发生的事情) )

I want the intial view to be the table view controller(tvc) and i want the Post view controller(pvc) to be to its left. I can load it up and swipe between the two easily but sometime i can swipe further than the boundaries so basically if i start on the tvc-->pvc-->tvc then i can go further by swiping right to the same tvc then the boundary is reached or go the other way tvc-->pvc-->tvc-->pvc-->pvc by swiping left for the last transition. The boundaries are reached in the end. Once the app intially launches at tvc i cannot go forward a page, (which is what should happen) and if i go tvc-->pvc and try to go to a page to the left it wont let me (which is what should happen)

谢谢

推荐答案

最后完成它,没有将任何属性添加到不同的类。刚试过课。下面是代码:

Finally done it, didn't have to add any properties to the different classes. Just tested the class. Heres the code:

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[MainTableViewController class]]) {
        MainPostController *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPostController"];
        return  pvc;

    }

    else return nil;

}


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

    if ([viewController isKindOfClass:[MainPostController class]]) {
        MainTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
        return tvc;

    }
    else return nil;
}

应该很容易添加更多视图控制器,因为我只需添加其他内容用于测试它们的陈述。解决方法的主要问题是方法isKindOfClass:

Should be easy to add more view controllers as i just need to add else if statements to test them. The main problem solver was the method isKindOfClass:

感谢您的回复!

这篇关于UIPageViewController与不同的ViewControllers,正确的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆