UIPageViewController 在静态视图控制器和保持(重用)内容之间切换

Posted

技术标签:

【中文标题】UIPageViewController 在静态视图控制器和保持(重用)内容之间切换【英文标题】:UIPageViewController switch between static view controllers and keep(reuse) contents 【发布时间】:2014-01-04 12:47:46 【问题描述】:

我有三个视图控制器:Settings、Chatbox 和 QuickMsg。

我想用 UIPageViewController 在这三个视图控制器之间切换。对于每个视图控制器,我不想在滑动页面之间重新启动视图控制器,因为它包含用户输入的数据并且我想保留数据。我将三个视图控制器定义为静态变量。

当我在页面之间滑动时屏幕变黑。我哪里做错了?

提前非常感谢!

ChatboxController、SettingController 和 QuickMsgController 是三个 UIViewController 控制器。 PagingViewController.h

#import <UIKit/UIKit.h>
#import "ChatboxController.h"
#import "SettingController.h"
#import "PagingViewController.h"
#import "QuickMsgController.h"
@class ChatboxController;
@class SettingController;
@class QuickMsgController;
@interface PagingViewController : UIViewController<UIPageViewControllerDataSource>

@property (strong, nonatomic) UIPageViewController *pageController;

@property (assign, nonatomic) NSInteger index;
+(ChatboxController*) getChatboxController;
+(SettingController*) getSettingsController;
@end

PagingViewController.m

#import "PagingViewController.h"

@interface PagingViewController ()

@end

@implementation PagingViewController

static ChatboxController* chatboxViewController;
static SettingController* settingViewController;
static QuickMsgController* quickMessageViewController;
int defaultIndex=1;
NSArray *viewControllers;
+(ChatboxController*) getChatboxController

    return chatboxViewController;

+(SettingController*) getSettingsController

    return settingViewController;

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


    self.index--;
    if(self.index<0)
    
        self.index=0;
    
    return [self viewControllerAtIndex:self.index];


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

    self.index++;

    return [self viewControllerAtIndex:self.index];


- (UIViewController *)viewControllerAtIndex:(NSUInteger)index 
    NSLog(@"index:%d",index);
    if(index==1)
    
        return chatboxViewController;
    
    else if (index==0)
        return settingViewController;
    else if(index==2)
    
         NSLog(@"Quick Message");
        return quickMessageViewController;
    else
        NSLog(@"error here");
        return nil;
    


- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController 
    // The number of items reflected in the page indicator.
    return 3;


- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController 
    // The selected item reflected in the page indicator.
    return defaultIndex;


- (void)viewDidLoad

    [super viewDidLoad];
    chatboxViewController= [self.storyboard instantiateViewControllerWithIdentifier:@"chatbox"];
    settingViewController= [self.storyboard instantiateViewControllerWithIdentifier:@"settings"];
    quickMessageViewController=[self.storyboard instantiateViewControllerWithIdentifier:@"quickmessages"];
    self.index=defaultIndex;

  //  [self performSelector:@selector(loadingNextView) withObject:nil afterDelay:2.0f];
    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

    self.pageController.dataSource = self;
    [[self.pageController view] setFrame:[[self view] bounds]];
    NSArray *viewControllers = [NSArray arrayWithObject:chatboxViewController];
    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    [self addChildViewController:self.pageController];
    [[self view] addSubview:[self.pageController view]];
    [self.pageController didMoveToParentViewController:self];




- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

【问题讨论】:

【参考方案1】:

将您的内容视图控制器放在NSArray

@property (nonatomic, strong) NSArray *contentViewControllers;

用途:


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

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

    if (index == 0) 
        return nil;
    

    return self.contentViewControllers[index - 1];


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

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

    if (index >= self.contentViewControllers.count - 1) 
        return nil;
    

    return self.contentViewControllers[index + 1];


- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController 
    return self.contentViewControllers.count;


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

【讨论】:

您好 Danilo,感谢您的回复。有用!另一个简单的问题,如何获得底部页面指示器的高度? 没有访问页面指示器的 Public API。如果这是一个问题,您可以滚动自己的页面指示器。

以上是关于UIPageViewController 在静态视图控制器和保持(重用)内容之间切换的主要内容,如果未能解决你的问题,请参考以下文章

使用 Swift 在 UIPageViewController 子视图之间传递数据

Xcode 8 - 如何在情节提要中完全实现 UIPageViewController?

如何在使用 UIpageviewcontroller 导航页面时从另一个视图控制器自动更新视图控制器上的标签

带有表格视图的 UIPageViewController,在 UITableView 内

图像的简单横向滚动 - UIScrollView 或 UIPageViewController?

UIPageControl(页面控件)不显示在 UIPageViewController