iPhone 子视图加载和等待
我的 iPhone 应用程序在其欢迎视图控制器上有一个子视图。子视图解析来自网站的数据并相应地加载数据。欢迎视图控制器有一个继续按钮,可以转到下一个视图控制器。但在子视图加载其数据之前,我无法转到下一个视图控制器。
任何人都可以建议我对此有任何解决方案吗?提前致谢。
My iPhone app has a sub view on its Welcome View Controller. The sub view parse data from a website and load data accordingly. Welcome View Controller has a continue button to go to the next view controller. But until the sub view load its data I cannot go to the next view controller.
Can anyone suggest me any solution on this. Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
NSURLConnection
/NSURLRequest
来检索数据,我建议采用两种方法:修改用于检索数据的类的代码,以便发出请求异步;这将使其成为非阻塞,并且用户将能够无需等待就可以继续下一步;下一步时,您可以选择取消请求,以节省带宽;
在单独的线程中执行请求;您可以使用
NSTask
或 GCDdispatch_asinc
来完成此操作,如下所示;无论如何,在这种情况下,您必须意识到您的单独线程可能不会修改UI(即使用UIKit),因为这只能从主线程完成。因此,在您的线程中,您更新数据,然后在主线程上刷新 UI(通过使用performSelectorOnMainThread
)。In you are using
NSURLConnection
/NSURLRequest
to retrieve the data, I would suggest two approaches:modify the code your class that retrieves the data so that the request is made asynchronous; this will make it non-blocking and the user will be able to move next without waiting; when moving next, you'll have the option of canceling the request, so to save bandwidth;
perform the request in a separate thread; you can do that either using
NSTask
or GCDdispatch_asinc
like shown below; in this case anyway, you have to be aware of the fact that your separate thread may not modify the UI (i.e., use UIKit), because this can only be done from the main thread. So, in your thread, you update the data, but then issue a refresh of the UI on the main thread (by usingperformSelectorOnMainThread
).正如 Vince 指出的,您应该从单独的线程中检索数据(创建一个工作类来为您执行此操作)。当类完成其工作时,您应该告诉欢迎视图数据已准备就绪。您可以通过使用协议或 NSNotification 来实现这一点。
As Vince pointed, you should retrieve the data from a separated thread (create a worker class to do that for you). When the classes finishes his job, you should tell the Welcome view, that the data is ready. You could achieve that, by using a protocol, or NSNotification.