尝试从plist和刷新屏幕加载数据

时间:2013-02-22 16:51:05

标签: ios uiview crash 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>
</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>
</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]; 

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"]];

    self.questionasked.text =question[currentQuestion];
    self.answer1.text = A[currentQuestion];
    self.answer2.text = B[currentQuestion];
    self.answer3.text = C[currentQuestion];
    }
}

-(IBAction)nextQ:(UIButton *) sender{
[self showNextQuestion];

}

该按钮被声明为一个动作,当按下它时,它只是在视图控制器周围循环

1 个答案:

答案 0 :(得分:0)

我发现您的代码至少存在一个问题:

 rootArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]
 pathForResource:@"question" ofType:@"plist"]];

为您提供一个自动释放的对象,当您离开viewDidLoad

范围时会释放该对象

尝试将rootArray变为属性