线程是否会干扰程序的正常工作行为?
我有一个视图控制器,它是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有 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.
您应该仅从主线程使用 UI。
所以在你的委托方法中你必须添加类似的东西
或者你可以直接调用tableView的方法:
You should work with UI only from the main thread.
So in your delegate method you have to add something like
Or you can call a method of tableView directly: