线程是否会干扰程序的正常工作行为?

发布于 2024-11-09 11:33:14 字数 1113 浏览 4 评论 0原文

我有一个视图控制器,它是 UIViewController 的子类,其中包含表视图,并且表视图中的每一行都链接到不同的 xml url。我创建了一个解析器类,它是 NSOperation 的子类,并实现了在选择每一行时解析 XML 文件的方法,

   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSelectorOnMainThread:@selector(pushView) withObject:nil waitUntilDone:NO];
[self performSelectorInBackground:@selector(parseOperation:) withObject:indexPath];
}

-(void)pushView{
detailView = [[viewDetailsController alloc] initWithNibName:@"viewDetailsController" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES]; 
}

 -(void)parseOperation:(NSIndexPath *)indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
self.queue = [[NSOperationQueue alloc] init];
parserClass *parser = [[parserClass alloc] initWithParseUrl:[[self.arrayOfUrls objectAtIndex:indexPath.row]delegate:self];
[queue addOperation:parser];
[parser release];
[pool release];
}

解析器工作得很好,但在其自定义委托方法中,我调用了将视图控制器推送到导航控制器堆栈的顶部,视图控制器正确初始化,但新的视图控制器未推送到屏幕中。 我不知道为什么这不起作用。

I have a view controller that is sub class of UIViewController which has table view into it and each rows in table view is linked to distinct xml url. I made a parser class that is sub class of NSOperation and implemented methods to parse the XML file on selection of each row as,

   -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self performSelectorOnMainThread:@selector(pushView) withObject:nil waitUntilDone:NO];
[self performSelectorInBackground:@selector(parseOperation:) withObject:indexPath];
}

-(void)pushView{
detailView = [[viewDetailsController alloc] initWithNibName:@"viewDetailsController" bundle:nil];
[self.navigationController pushViewController:detailView animated:YES]; 
}

 -(void)parseOperation:(NSIndexPath *)indexPath{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
self.queue = [[NSOperationQueue alloc] init];
parserClass *parser = [[parserClass alloc] initWithParseUrl:[[self.arrayOfUrls objectAtIndex:indexPath.row]delegate:self];
[queue addOperation:parser];
[parser release];
[pool release];
}

Parser works great but in its custom delegate method I have called to push view controller on the top of the navigation controller stack, the view controller initializes correctly but the new view controller is not pushed into the screen.
I dont have any idea about why this is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

垂暮老矣 2024-11-16 11:33:14

所有 UI 调用都必须从主线程进行,其中可能包括分配/初始化 UI 类。听起来您在自定义委托方法中违反了该规则。

All UI calls must be made from the main thread, which may include allocating/initializing UI classes. It sounds like you are breaking that rule in your custom delegate method.

只是在用心讲痛 2024-11-16 11:33:14

您应该仅从主线程使用 UI。
所以在你的委托方法中你必须添加类似的东西

[self performSelectorOnMainThread:@selector(showImage:) withObject:aImage waitUntilDone:NO];

或者你可以直接调用tableView的方法:

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

You should work with UI only from the main thread.
So in your delegate method you have to add something like

[self performSelectorOnMainThread:@selector(showImage:) withObject:aImage waitUntilDone:NO];

Or you can call a method of tableView directly:

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