UIPageViewController with different ViewControllers, right?

Basically, I have a Table View Controller and a regular View Controller, I want to make it so that I can scroll between view controllers, for example, on the main application screen (obviously, the same type of controller).

But what I find is that UIpageviewcontrollerusaully is used, when you have several views of the same type viewcontroller, for example, scrolling between images, you can just set the index in the class and the image array related to the index, and configure him that way.

But I'm trying to get different view controllers in this setting. Now it’s two, but it will certainly reach 4.

Is the UIpageviewcontrollerright way to customize it or something else.

EDIT: code added below

    //
//  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

I want the intial view to be the table view controller (tvc), and I want the Post view controller (pvc) to be on its left. I can download it and scroll between the two easily, but someday I can move beyond the borders, so if I start with tvc → pvc → tvc, then I can go further by checking directly to the same TV, then the border is reached or goes another way tvc → pvc → tvc → pvc → pvc, scrolling left for the last transition. Boundaries are reached at the end. When the application starts in tvc, I cannot advance the page (which is what should happen), and if I go tvc → pvc and try to go to the page on the left, this will not allow me (which is what should happen)

thanks

+4
4

, - . . :

-(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;
}

, else, . isKindOfClass:

, !

+8

, .

.h

@interface ViewController : UIViewController <UIPageViewControllerDataSource>

@property (strong, nonatomic) UIPageViewController *pageViewController;

@property (nonatomic) NSArray *viewControllerIDArr;

@end

.m

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setupDefaults];
}

- (void)setupDefaults
{
    self.viewControllerIDArr = @[@"FirstViewController", @"SecondViewController"];

    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];

    self.pageViewController.dataSource = self;

    UIViewController *startingViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.viewControllerIDArr[0]];

    NSArray *viewControllers = @[startingViewController];

    [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 - 45);

    [self addChildViewController:_pageViewController];

    [self.view addSubview:_pageViewController.view];

    [self.pageViewController didMoveToParentViewController:self];
}

- (UIViewController *)getViewControllerFromClass:(Class)class isNext:(BOOL)next
{
    NSInteger index = [self.viewControllerIDArr indexOfObject:[NSString stringWithFormat:@"%@", class]];

    if (next)
        index+=1;
    else
        index-=1;

    if (index < 0 || index >= self.viewControllerIDArr.count)
        return nil;
    else
        return [self.storyboard instantiateViewControllerWithIdentifier:self.viewControllerIDArr[index]];

}


#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    return [self getViewControllerFromClass:viewController.class isNext:NO];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    return [self getViewControllerFromClass:viewController.class isNext:YES];
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return 0;
}

:

self.viewControllerIDArr viewControllers.

:

enter image description here

+2

, , .

, ,

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { }

, didFinishAnimating , .

.

func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {

    let currentView = pendingViewControllers[0]

    if currentView is UINavigationController && currentView.restorationIdentifier == "Settings" {
        index = 0
    } else if currentView is UINavigationController && currentView.restorationIdentifier == "Main" {
        index  = 1
    } else if currentView is UINavigationController && currentView.restorationIdentifier == "Message" {
        index = 2
    }
}

currentView UIViewController ( UINavigationController , , restoreIdentifier ), index - , , UIViewController .

+1

UIViewPageViewController .

, , .

pageNumber , . , . switch . switch .

-1

All Articles