我正在尝试从plist创建一个循环,具体取决于数组中的项目数量,这是我的plist代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>question</key>
<string>2 + 2 = ?</string>
<key>A</key>
<string>2</string>
<key>B</key>
<string>4</string>
<key>C</key>
<string>3</string>
<key>Ans</key>
<string>B</string>
</dict>
<dict>
<key>question</key>
<string>2 + 4 = ?</string>
<key>A</key>
<string>3</string>
<key>B</key>
<string>6</string>
<key>C</key>
<string>9</string>
<key>Ans</key>
<string>B</string>
</dict>
</array>
</plist>
所以我的数组中有两个项目。我希望屏幕加载第一个项目并显示它然后使用界面中的按钮再次使用第二个项目列表显示屏幕
- (void)viewDidLoad
{
[super viewDidLoad];
rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"question" ofType:@"plist"]];
currentQuestion = -1;
[self showNextQuestion];
}
-(void) showNextQuestion{
currentQuestion++;
int numItems = [rootArray count];
NSMutableArray *question = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *A = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *B = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *C = [NSMutableArray arrayWithCapacity:numItems];
NSMutableArray *CorrectAns = [NSMutableArray arrayWithCapacity:numItems];
for (NSDictionary *itemData in rootArray) {
[question addObject:[itemData objectForKey:@"question"]];
[A addObject:[itemData objectForKey:@"A"]];
[B addObject:[itemData objectForKey:@"B"]];
[C addObject:[itemData objectForKey:@"C"]];
[CorrectAns addObject:[itemData objectForKey:@"Ans"]];
self.questionasked.text =question[currentQuestion];
self.answer1.text = A[currentQuestion];
self.answer2.text = B[currentQuestion];
self.answer3.text = C[currentQuestion];
}
}
我知道我必须创建某种循环并使用动作,但我不确定如何
答案 0 :(得分:1)
你已经拥有了rootArray中的所有内容,所以你不需要使用NSMutableArrays完成所有这些工作。做一些像
这样的事情NSDictionary *item = rootArray[currentQuestion];
self.questionasked.text = item[@"question"];
self.answer1.text = item[@"A"];
并设置一个调用[self showNextQuestion]的UIButton;当你点击它时。