如何在执行计算任务之前将视图添加到视图中
问题描述:
我有一个UITableViewController,当我将一个特定的视图压入堆栈时,它会一直持续下去,所以我想在移动之前向该单元添加一个微调器。我遇到的问题是在新视图被压入控制器堆栈之后,微调器被添加。但我认为这些消息是同步的?如何在执行计算任务之前将视图添加到视图中
那么如何才能在进入下一个视图之前使这个微调器显示?提前致谢!
- (void)
tableView: (UITableView *) tableView
didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
UITableViewCell *cell = [ self.tableView cellForRowAtIndexPath: indexPath ];
if (cell.accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
UIActivityIndicatorView *activityIndicator = [ [ UIActivityIndicatorView alloc ] initWithFrame: CGRectMake(260.0, 10.0, 25.0, 25.0) ];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[ cell addSubview: activityIndicator ];
[ activityIndicator startAnimating ];
TableDemoViewController *newViewController = [ [ TableDemoViewController alloc ] initWithPath: cell.textLabel.text ];
UINavigationController *navigationController = [ appDelegate navigationController ];
[ navigationController pushViewController: newViewController animated: YES ];
[ activityIndicator stopAnimating ];
}
}
答
如果简单地启动TableDemoViewController是如此的密集,你还有其他问题。请记住,正如你所指出的那样,主线程是同步的。你有几个选项:
- 有可能只需在添加微调器后旋转一次运行循环就可以解决您的问题。你可以通过将你的方法分成两部分,然后使用TableDemoViewController init从
performSelector:withObject:afterDelay:
中调用第二部分,延迟为0. - 你可能想知道init为什么如此昂贵,主线程。我强烈建议学习如何使用NSOperations和NSOperationQueue。您可以使用代理模式或NSNotificatin让呼叫者知道您的昂贵操作何时完成。
一般来说,如果你有东西阻塞在主线程上的处理,这很糟糕。您希望尽快将其从主线程移出。主线程是输入事件和动画发生的地方 - 主线程*运行的能力使您的应用程序响应迅速。请记住,即使用户无法推进应用程序的状态,也可能有很多她可以互动并且可以看到很多。
感谢您的支持,您完全正确,应该将其从主线程中移除。非常感谢! – 2010-03-02 17:07:52