在视图之间传递字符串:应用程序崩溃
基本上,我正在创建一个应用程序,其主视图是 MKMapView,我需要在其中显示一些注释(企业、学校等)。注释来自我从网站“动态”检索的 KML 文件。第二个视图是UITableView,具有代表地图中注释的类别。选择某些类别将产生一个带有其 ID 的字符串,当单击“后退”按钮时,此视图应将此字符串传递给 MapView,并在其中连接到另一个字符串保存“查询字符串”的其他部分,因此应该下载 KML 文件并在地图中查看其注释。 有一个问题,所有单元格都可以选择(选中),但是当我单击导航栏按钮“后退”转到上一个视图时,应用程序崩溃。当我不选择任何单元格并单击“后退”按钮时,应用程序仍然崩溃,但在日志文件中它告诉我传递给前一个视图的字符串为零。我已经准备好将参数从一个视图传递到另一个视图的代码,但不明白出了什么问题.我发布的代码仅与传递相关视图之间的字符串。
GisListViewController.m:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark ) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSMutableDictionary * myDictionary = [[NSMutableDictionary alloc] initWithCapacity:60];
int i = 0;
for (i = 0; i < [gisCategoryID count]; i++) {
[myDictionary setObject:[gisCategoryList objectAtIndex:i] forKey:[gisCategoryID objectAtIndex:i]];
NSMutableString *paramString2 = [[[NSMutableString alloc] init] autorelease];
[paramString2 appendFormat:@"%@&", [myDictionary objectForKey:[gisCategoryID objectAtIndex:i]]];
paramString = paramString2;
}
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void) viewWillDisappear:(BOOL) animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSString *httpString = @"http://www.ikub.al/hartav2/handlers/kmlgenerator.ashx?layerid=";
NSString *finalkmlString = [ httpString stringByAppendingString:paramString ];
[[self delegate] setKmlString:finalkmlString];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
希望有帮助。
Basically I am creating an app with the main view being a MKMapView where I need to show some annotations(of businesses, schools etc.).The annotations come from KML files that I retrieve 'dynamically' from a website.The second view, an UITableView, has the categories that represent annotations in the map.The selection of some categories, will lead to a string with their ID's and when the Back button is clicked, this view should pass this string to the MapView, where it gets concatenated to another string that holds the other part of the 'query string', and so the KML file should be downloaded and its annotations viewed in the map.
There's a problem,all the cells can be selected (checked), but when I click the Navigation Bar button 'Back' to go to the previous view, the app crashes.When I don't select any cell and click the Back button, the app still crashes, but in the log file it tells me that the string I was passing to the previous view is nil.I have prepared the code for passing the parameter from one view to another, and don't understand what is going wrong.I am posting code relevant only to the passing of the string between views.
GisListViewController.m :
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark ) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSMutableDictionary * myDictionary = [[NSMutableDictionary alloc] initWithCapacity:60];
int i = 0;
for (i = 0; i < [gisCategoryID count]; i++) {
[myDictionary setObject:[gisCategoryList objectAtIndex:i] forKey:[gisCategoryID objectAtIndex:i]];
NSMutableString *paramString2 = [[[NSMutableString alloc] init] autorelease];
[paramString2 appendFormat:@"%@&", [myDictionary objectForKey:[gisCategoryID objectAtIndex:i]]];
paramString = paramString2;
}
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
- (void) viewWillDisappear:(BOOL) animated
{
NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
NSString *httpString = @"http://www.ikub.al/hartav2/handlers/kmlgenerator.ashx?layerid=";
NSString *finalkmlString = [ httpString stringByAppendingString:paramString ];
[[self delegate] setKmlString:finalkmlString];
NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
Hope it helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜 Autorelease 给出了问题。尝试这样
I guess Autorelease giving the problem.Try like this
如果您正在使用自动释放,那么您应该进行如下所示的分配:-
如果您不使用自动释放,那么您应该使用以下分配以避免内存泄漏。
希望这有帮助...
If you are using autorelease then you should do assignment as shown below:-
and if you are not using autorelease then you should use following assignment to avoid memory leaks.
Hope this helps...