我有一个应用程序,它有一个 tabbarcontroller,每个选项卡都包含一个 navigationcontroller。每个导航控制器都以名为 DetailViewController 的视图控制器结束。但是,如果用户在一个选项卡上导航到 DetailViewController,然后切换选项卡并在那里也导航到 DetailViewController,那么应用程序会因错误而崩溃:
2011-08-19 21:14:45.257 Kluringar[4368:ef03] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 0]'
推送 DetailViewController 的代码如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
kluringIdString = [[kluringar objectAtIndex:indexPath.row] objectForKey:@"id"];
DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];
[self.navigationController pushViewController:detail animated:YES];
}
从数据库中获取数据并将其添加到数组中的代码如下所示:
-(void) getAllRowsFromTableNamed {
//—-retrieve rows—-
NSString *qsql = [NSString stringWithFormat:@"SELECT * FROM Kluringar ORDER BY rubrik"];
sqlite3_stmt *statement;
kluringar = [[NSMutableArray alloc] init];
NSMutableDictionary *kluringarDictionary = [[NSMutableDictionary alloc] init];
if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *kluringId = (char *) sqlite3_column_text(statement, 2);
NSString *kluringIdStr = [[NSString alloc] initWithUTF8String: kluringId];
char *rubrik = (char *) sqlite3_column_text(statement, 3);
NSString *rubrikStr = [[NSString alloc] initWithUTF8String: rubrik];
//---add items---
[kluringarDictionary setValue:kluringIdStr forKey:@"id"];
[kluringarDictionary setValue:rubrikStr forKey:@"rubrik"];
[kluringar addObject:[kluringarDictionary copy]];
}
//—-deletes the compiled statement from memory—-
sqlite3_finalize(statement);
}
}
这是故事板的链接:storyboard
提前比!:)