我有三个视图控制器:设置,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
答案 0 :(得分:5)
将您的内容视图控制器放在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;
}