我设法从我的question.plist上读取了我的问题作为问答。
self.view.backgroundColor = [UIColor colorWithRed:99.0/255.0 green:162.0/255.0 blue:223.0/255.0 alpha:1.0];
self.title = @"Game"; //Set title.
// Path to the plist (in the application bundle)
NSString *path = [[NSBundle mainBundle] pathForResource:
@"Question" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSDictionary *dict = [array objectAtIndex:0];
questionlabel.text = [dict valueForKey:@"Question"];这里的一个按钮调用二维码扫描仪,扫描后将检查答案是否正确,并将用户引导到question.plist中的另一个问题(objectAtIndex:1),如果正确,则会弹出并显示错误
我可以用静态的方式创建更多的到xib的问题来静态地创建其他问题,但是我知道这是一种麻烦的方式。有人知道我怎样才能以动态的方式做到这一点吗?
感谢预付款
德斯蒙德
发布于 2011-07-15 19:43:25
二维码扫描器和什么都有关系?我认为这与你的问题并不严格相关,所以我只会确切地介绍你所问的问题。二维码扫描不是iOS的一部分,由外部库提供。
向视图控制器添加一个整数i-var:
@interface WhateverYouNamedYourViewController : UIViewController {
// ....
int currentQuestion;
// ....
}
// ....
@end然后,在加载问题和更新UI时,使用该i-var构造从中加载问题的NSString。
NSString *questionFileName = [NSString stringWithFormat:
@"Question%d", currentQuestion];
NSString *path = [[NSBundle mainBundle] pathForResource:questionFileName
ofType:@"plist"];或者,考虑将所有问题存储在根为数组的单个plist中。然后使用NSArray的-objectAtIndex:方法获取当前问题(只需将currentQuestion作为第一个参数传递)。
在任何情况下,检查答案正确性的地方(测验中)或记录答案的地方(调查问卷中)都需要更新当前问题并更新UI:
currentQuestion++;
[self updateUI];更新UI读取下一个问题,并相应地更新标签和按钮。
https://stackoverflow.com/questions/4611309
复制相似问题