我正在开发一个测验应用程序,在那里我从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
}
}
感谢您的帮助。
答案 0 :(得分:1)
在这种情况下,您需要使用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了解详情。
答案 1 :(得分:0)
另一张海报关于使用页面视图控制器的建议很好。这样可以让你以一种干净利落的方式管理页面上的问题。
关于表单上可变数量字段的问题:
最简单的方法是为最大数量的问题构建表单。然后,当您显示它时,隐藏那些不用于当前问题的按钮/图像。
您可以为每个按钮/图像创建插座,然后直接操作它们,或者您可以为它们指定标签编号。如果使用标签号,则编写显示正确答案按钮数的代码会更容易一些。
或者,您可以编写代码来动态创建视图控制器层次结构并将其添加到视图控制器层次结构中,但直接在代码中创建视图对象比使用IB执行更多的工作。这比我在论坛帖子中详细解释的要多。为此,您应该搜索有关通过代码而不是IB创建按钮的教程。它最终像每个按钮4或5行代码。
步骤的大致轮廓:您必须使用initWithFrame或UIButton buttonWithType类方法创建一个按钮。您必须向按钮添加目标/操作。你必须设置它的标题。你必须设置它的框架,你必须在实例变量或数组中保存它的指针,你必须将它作为子视图添加到视图控制器的内容视图。