在视图之间传递字符串:应用程序崩溃

发布于 2024-12-10 17:16:19 字数 2046 浏览 1 评论 0原文

基本上,我正在创建一个应用程序,其主视图是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-12-17 17:16:19

我猜 Autorelease 给出了问题。尝试这样

NSMutableString *paramString2 = [[NSMutableString alloc] init];

I guess Autorelease giving the problem.Try like this

NSMutableString *paramString2 = [[NSMutableString alloc] init];
美男兮 2024-12-17 17:16:19

如果您正在使用自动释放,那么您应该进行如下所示的分配:-

paramString = [paramString2 retain];

如果您不使用自动释放,那么您应该使用以下分配以避免内存泄漏。

paramString = [paramString2 copy];

希望这有帮助...

If you are using autorelease then you should do assignment as shown below:-

paramString = [paramString2 retain];

and if you are not using autorelease then you should use following assignment to avoid memory leaks.

paramString = [paramString2 copy];

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文