以编程方式在UIPageViewController中导航页面

时间:2013-05-24 13:23:16

标签: ios architecture interface-builder uipageviewcontroller

我正在尝试实施调查问卷应用。

我有一个UIPageViewController附加到名为QuizPageViewController.m的主视图控制器。其他控制器(如按钮)放置在名为QuizContentViewController.m的内容视图控制器中。

现在我的问题是如何从QuizContentViewController.m以编程方式导航UIPageViewController的页面(例如,当clickDoneClicked点击时)?我已经意识到我可以使用以下命令以编程方式导航页面但我的问题是我无法从内容视图控制器(QuizContentViewController)访问其参数。

setViewControllers:方向:动画:完成:

以下是我的代码。

QuizPageViewController.h

#import <UIKit/UIKit.h>
#import "QuizContentViewController.h"

@class QuizPageViewController;

@protocol QuizPageViewControllerDelegate <NSObject>

@optional // Delegate protocols

- (void)dismissReaderViewController:(QuizPageViewController *)viewController;

@end

@interface QuizPageViewController : UIViewController <UIPageViewControllerDataSource, QuizContentViewControllerDelegate>{

}

- (void)moveForward:(id)sender;
- (void)moveBackwards:(id)sender;
- (void)abort:(id)sender;

@property (nonatomic, unsafe_unretained, readwrite) id <QuizPageViewControllerDelegate> delegate;
@property (nonatomic, strong) UIPageViewController *pageView;

@end

QuizPageViewController.m

#import "QuizPageViewController.h"

@interface QuizPageViewController ()

@end

@implementation QuizPageViewController
@synthesize pageView, delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [self QuestionDet];
    [super viewDidLoad];
// Do any additional setup after loading the view.

    NSDictionary *option = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
    pageView = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:option];
    [pageView setDataSource:self];

    QuizContentViewController *initialVC = [self viewControllerAtIndex:0];
    NSArray *viewControllers = [NSArray arrayWithObject:initialVC];
    [pageView setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
CGRect viewRect = self.view.bounds;
    [[pageView view] setFrame:viewRect];
    [self addChildViewController:self.pageView];
    [self.view addSubview:[pageView view]];
    [pageView didMoveToParentViewController:self];
}

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

- (NSInteger) indexOfViewController:(QuizContentViewController *)viewController{
    return viewController.dataObjquizNo;
}


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

    int index = [self indexOfViewController:(QuizContentViewController *)viewController];
    if (index == 0 || index == NSNotFound) {
        return nil;
    }

    index --;
    return [self viewControllerAtIndex:index];
}


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

    int index = [self indexOfViewController:(QuizContentViewController *)viewController];
    if (index == NSNotFound) {
    return nil;
    }
    index++;
    return [self viewControllerAtIndex:index];
}

- (QuizContentViewController *)viewControllerAtIndex:(NSInteger)index{

    if (index > [quizdet count] - 1) {
    return nil;
    }

    QuizContentViewController *cVC = [[QuizContentViewController alloc] init];
    cVc.delegate = self;

    Questions *quizz =  [quizdet objectAtIndex:index];

    [cVC setDataObjQuiz:[quizz quiz]];
    [cVC setDataObjAns1:[quizz answer1]];
    [cVC setDataObjAns2:[quizz answer2]];
    [cVC setDataObjAns3:[quizz answer3]];
    [cVC setDataObjAns4:[quizz answer4]];
    [cVC setDataObjquizNo:index];
    [cVC setDataObjtotalNoOfQuiz:[quizdet count]];

    return  cVC;
}

- (void)moveForward:(id)sender{
    // Navigation forward code should goes here...
}
- (void)moveBackwards:(id)sender{
    // Navigation backwards code should goes here...
}
- (void)abort:(id)sender{
    [delegate dismissReaderViewController:self];
}


}

QuizContentViewController.h

#import <UIKit/UIKit.h>

@class QuizContentViewController;
@protocol QuizContentViewControllerDelegate <NSObject>

- (void)moveForward:(id)sender;
- (void)moveBackwards:(id)sender;
- (void)abort:(id)sender;

@end

@interface QuizContentViewController : UIViewController{
    UITextView *txtVwQuiz;
    UILabel *lblSummery;

    NSString *dataObjQuiz;
    NSString *dataObjAns1;
    NSString *dataObjAns2;
    NSString *dataObjAns3;
    NSString *dataObjAns4;

    NSInteger dataObjquizNo;
    NSInteger dataObjtotalNoOfQuiz;

}

@property(nonatomic, unsafe_unretained, readwrite) id <QuizContentViewControllerDelegate> delegate;

@property (nonatomic, strong) NSString *dataObjQuiz;
@property (nonatomic, strong) NSString *dataObjAns1;
@property (nonatomic, strong) NSString *dataObjAns2;
@property (nonatomic, strong) NSString *dataObjAns3;
@property (nonatomic, strong) NSString *dataObjAns4;

@property NSInteger dataObjquizNo;
@property NSInteger dataObjtotalNoOfQuiz;


@end

QuizContentViewController.m

#import "QuizContentViewController.h    
#import <QuartzCore/QuartzCore.h>


#define isPhone568 ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568)
#define iPhone568ImageNamed(image) (isPhone568 ? [NSString stringWithFormat:@"%@-568h.%@", [image stringByDeletingPathExtension], [image pathExtension]] : image)
#define iPhone568Image(image) ([UIImage imageNamed:iPhone568ImageNamed(image)])

# define IS_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone

@interface QuizContentViewController ()

@end

@implementation QuizContentViewController
@synthesize dataObjQuiz, dataObjAns1, dataObjAns2, dataObjAns3, dataObjAns4, dataObjquizNo, dataObjtotalNoOfQuiz, delegate;


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor clearColor]; // Transparent
    UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iPhone568ImageNamed(@"QuizPage.png")]];

    [self.view addSubview:backgroundImage];
    [self.view sendSubviewToBack:backgroundImage];


// Do any additional setup after loading the view.
    // Override point for customization after application launch
    CGSize viewSize = self.view.bounds.size;

    float boader;
    float heightofSubVw;

    boader = IS_IPAD?15:37;
    heightofSubVw = viewSize.height/10;

    lblSummery = [[UILabel alloc] initWithFrame:CGRectMake(boader, 5, viewSize.width-(2*boader), heightofSubVw*1)];

    [lblSummery setBackgroundColor:[UIColor clearColor]];
    [lblSummery setTextAlignment:UITextAlignmentLeft];
    [lblSummery setFont:[UIFont systemFontOfSize:IS_IPAD?14.f:28.f]];
    [lblSummery setText:[NSString stringWithFormat:@" Question %d out of %d.",dataObjquizNo+1, dataObjtotalNoOfQuiz]];
    [lblSummery setTextColor:[UIColor orangeColor]];
    [self.view addSubview:lblSummery];


    txtVwQuiz = [[UITextView alloc] initWithFrame:CGRectMake(boader, heightofSubVw, viewSize.width-(2*boader), heightofSubVw*4)];

    [txtVwQuiz setText:dataObjQuiz];
    [txtVwQuiz setBackgroundColor:[UIColor clearColor]];
    [txtVwQuiz setTextAlignment:UITextAlignmentLeft];
    [txtVwQuiz setTextColor:[UIColor whiteColor]];
    [txtVwQuiz setFont:[UIFont systemFontOfSize:IS_IPAD?15.f:30.f]];
    [txtVwQuiz setEditable:NO];
    [self.view addSubview:txtVwQuiz];

    NSArray *options =[[NSArray alloc]
                   initWithObjects:dataObjAns1,dataObjAns2,dataObjAns3,dataObjAns4,nil];



    MIRadioButtonGroup *group =[[MIRadioButtonGroup alloc]
                            initWithFrame:CGRectMake(boader, heightofSubVw*4, viewSize.width-(2*boader), heightofSubVw*5)
                            andOptions:options andColumns:1];
    [self.view addSubview:group];



    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(boader+(IS_IPAD?6:12), heightofSubVw*8.5, IS_IPAD?44:88, IS_IPAD?44:88)];
    [btnBack addTarget:self action:
 @selector(navButtonBackClicked:)
 forControlEvents:UIControlEventTouchUpInside];
    btnBack.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [btnBack setBackgroundImage:[UIImage imageNamed:
                  @"Navigation_Back.png"] forState:UIControlStateNormal];
    [self.view addSubview:btnBack];

    UIButton *btnDone = [[UIButton alloc] initWithFrame:CGRectMake(viewSize.width - (boader+(IS_IPAD?10 + 44 :20 + 88)), heightofSubVw*8.5, IS_IPAD?44:88, IS_IPAD?44:88)];
    [btnDone addTarget:self action:
 @selector(navButtonDoneClicked:)
  forControlEvents:UIControlEventTouchUpInside];
btnDone.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentCenter;
    [btnDone setBackgroundImage:[UIImage imageNamed:
                             @"Navigation_Done.png"] forState:UIControlStateNormal];

    [self.view addSubview:btnDone];

    UIButton *btnAbort = [[UIButton alloc] initWithFrame:CGRectMake(viewSize.width - (boader+(IS_IPAD?2*10 + 88:2*20 + 176)), heightofSubVw*8.5, IS_IPAD?44:88, IS_IPAD?44:88)];
    [btnAbort addTarget:self action:
 @selector(navButtonAbortClicked:)
  forControlEvents:UIControlEventTouchUpInside];
    btnAbort.contentHorizontalAlignment =
    UIControlContentHorizontalAlignmentCenter;
    [btnAbort setBackgroundImage:[UIImage imageNamed:
                             @"Navigation_Abort.png"] forState:UIControlStateNormal];

    [self.view addSubview:btnAbort];

}

-(IBAction) navButtonDoneClicked:(UIButton *) sender{

    id index;
    index = [NSNumber numberWithInt:dataObjquizNo];

    if ([delegate respondsToSelector:@selector(moveForward:)]) {
        [delegate moveForward:index];
    }

}

-(IBAction) navButtonAbortClicked:(UIButton *) sender{

    id index;
    index = [NSNumber numberWithInt:dataObjquizNo];

    if ([self.delegate respondsToSelector:@selector(abort:)]) {
        [self.delegate abort:index];
    }

}

-(IBAction) navButtonBackClicked:(UIButton *) sender{

    id index;
    index = [NSNumber numberWithInt:dataObjquizNo];

    if ([delegate respondsToSelector:@selector(moveBackwards:)]) {
        [delegate moveBackwards:index];
    }

}

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,QuizContentViewController的视图有按下的按钮,一旦按下它需要通知你的mainviewcontroller,所以它可以在QuizPageViewController上适当地设置视图?如果是这种情况,协议是视图控制器向其他视图控制器发送消息的好方法。以下是有关协议的Apple文档:Working with Protocols

第一次使用时,协议有点棘手。一切设置正确后,您应该能够做到这样的事情:

-(void)buttonBackClicked:(id)sender
{
     [self.delegate shouldMoveBack];
}