我正在研究测验应用程序,我从api那里获得测验项目。现在,每个问题都包含一张图片和4个可能的答案。当前,我有两个附加到我的视图控制器的视图(前视图和后视图),当单击一个答案按钮时,它们分别变为另一个视图(我只以这种方式显示前两个问题)。
现在,每个api调用都会得到7个项目(每个项目1个图像和4个答案)。但是问题的数量可以随时更改,因此我试图实现一种自动执行视图创建过程的方法。
根据我的搜索,这是我能够想到的:
QuizMainViewController.h
#import <UIKit/UIKit.h>
@interface QuizMainViewController: UIViewController <UIScrollViewDelegate>
@property (nonatomic,retain)NSArray *results;
@property (nonatomic,assign)NSUInteger currentItemIndex;
@property(nonatomic,assign)NSUInteger numberOfItemsIntheResultsArray;
QuizMainViewController.m
#import "QuizMainViewController.m"
#import "AppDelegate.h"
#import "Base64.h"
#import "Item.h"
#import "SVProgressHUD.h"
#import <UIKit/UIKit.h>
@interface QuizMainViewController ()
@property(nonatomic,strong)IBOutlet UIView *frontView;
@property (nonatomic,strong)IBOutlet UIView *BackView;
//first view outlets
@property (strong, nonatomic) IBOutlet UIImageView *frontViewImage;
@property (nonatomic,weak) IBOutlet UIButton *frontViewAnswer1;
@property (nonatomic,weak) IBOutlet UIButton *frontViewAnswer2;
@property (nonatomic,weak)IBOutlet UIButton *frontViewAnswer3;
@property (nonatomic,weak) IBOutlet UIButton *frontViewAnswer4;
//second view outlets
@property (strong, nonatomic) IBOutlet UIImageView *backViewImage;
@property(nonatomic,weak) IBOutlet UIButton *backViewAnswer1;
@property(nonatomic,weak) IBOutlet UIButton *backViewAnswer2;
@property(nonatomic,weak) IBOutlet UIButton *backViewAnswer3;
@property(nonatomic,weak) IBOutlet UIButton *backViewAnswer4;
@implementation QuizMainViewController
//the method that I am making the api call
-(void)loadQuizObjectsMethod
{
// this is the part I make the call and try to set the views based on if the items.count is odd or even.
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *req, NSHTTPURLResponse *response, id jsonObject)
{
NSArray *array = [jsonObject objectForKey:@"Items"];
NSLog(@"Item array: %@",array);
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
for (NSDictionary * itemDict in array) {
Item *myResponseItems = [[Item alloc] initWithDictionary:itemDict];
[tempArray addObject:myResponseItems];
}
_results = tempArray;
//this is where I need help with
Item *item = [_results objectAtIndex:_numberOfItemsIntheResultsArray];
for (_numberOfItemsIntheResultsArray in _results) {
if (_currentItemIndex %2 == 0) {
_currentItemIndex
//implement the front view
}else if (_currentItemIndex %2 == 1)
{
//implement the back view
}
}
我感谢您的帮助。
最佳答案
在这种情况下,您需要使用UIPageViewController
类。首先创建一个名为QuestionViewController
的类,如下所示
QuestionViewController.h
@property (strong, nonatomic) IBOutlet UIImageView *image;
@property (nonatomic,weak) IBOutlet UIButton *answer1;
@property (nonatomic,weak) IBOutlet UIButton *answer2;
@property (nonatomic,weak)IBOutlet UIButton *answer3;
@property (nonatomic,weak) IBOutlet UIButton *answer4;
@property (strong, nonatomic) Item *questionDetailsFromJSON;
QuestionViewController.m
-(void)viewDidLoad
{
self.image = self.questionDetailsFromJSON.image
//set all remaining outlets similar to this.
}
在情节提要或xib中,使用
UIPageViewController
将QuizMainViewController
嵌入到UIContainer
中。将QuizMainViewController
设置为页面视图控制器的委托。如下更改您的QuizMainViewController.m
@interface QuizMainViewController ()
@property(nonatomic,weak)IBOutlet UIPageViewController *pvc;
@implementation QuizMainViewController
//Implement all of the other methods
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
int index = [self.results indexOfObject:[(QuestionViewController *)viewController question]];
index++;
QuestionViewController *qvc = nil;
if(index<self.results.count)
{
qvc = //Instantiate from story board or xib
qvc.questionDetailsFromJSON = [self.results objectAtIndex:index];
}
return qvc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
int index = [self.results indexOfObject:[(QuestionViewController *)viewController question]];
index--;
QuestionViewController *qvc = nil;
if(index>=0)
{
qvc = //Instantiate from story board or xib
qvc.questionDetailsFromJSON = [self.results objectAtIndex:index];
}
return qvc;
}
请遵循此tutorial以获得更多详细信息。