iPhone中如何让解析完成后出现tabbar视图?
我是 iPhone 开发新手。我创建了一个应用程序,其中第一个选项卡栏视图加载网页,在第二个选项卡栏视图中,它解析 xml 文件并在表视图中显示内容。
当我单击第二个选项卡栏时,只有在解析完成后才能看到选项卡栏视图,直到解析时间选项卡栏显示为未选中。我想在解析完成后显示带有活动指示器的选项卡栏视图。我怎样才能实现它?
I am new to iPhone development. I created an application in which the first tab bar view loads a web page and in second tab bar view, it parses a xml file and display the content in the table view.
When I click the second tab bar, the tab bar view is seen only after the parsing is done, till the parsing time the tab bar appears like unselected. I want to display the tabbar view with activity indicator when the parsing is done. How can I achieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果没有额外的信息很难判断,但我猜您正在
viewDidLoad()
或loadView(
) 内解析 XML 文件。然而,解析实际上可能需要时间,并且您正在阻塞负责更新 UI 的主线程。这就是为什么您只有在解析完成后才能看到选项卡栏的原因。为了解决这种情况,您需要推迟显示数据的表视图,显示活动指示器来告诉用户正在发生某些事情,并启动一个负责解析 XML 文件的后台线程。完成后处理 XML 文件的线程然后停止活动指示器并使用解析的数据设置表视图。
您可以使用可用的 API(
detachNewThreadSelector:toTarget:withObject:
和performSelectorOnMainThread:withObject:waitUntilDone:
等)轻松完成此操作,但是,您可能需要考虑利用MBProgressHUD 它提供了您所需的功能,包装在一个非常易于使用的类中。Without additional information about is difficult to tell, but I guess you are parsing the XML file inside
viewDidLoad()
orloadView(
). However, parsing may actually require time, and you are blocking the main thread which is responsible for updating the UI. This is the reason why you are seeing the tab bar only after parsing is completed.To remedy this situation, you need to defer your table view showing data, display an activity indicator to tell your users that something is going on, and start a background thread in charge of parsing your XML file.The thread processing the XML file once done then stops the activity indicator and setup your table view with the parsed data.
You can do this very easily using the API available (
detachNewThreadSelector:toTarget:withObject:
andperformSelectorOnMainThread:withObject:waitUntilDone:
etc), however, you may want to consider taking advantage of MBProgressHUD which provides exactly the functionality you need wrapped in a very easy to use class.我想您知道或能够弄清楚如何让活动指示器正常工作。
您可以使用 NSXMLparser 委托方法来解析 xml 数据。您可以在您创建的模型对象中收集 xml 数据,作为解析 xml 的类中的属性,即一个简单的数组。然后,从您希望收到解析完成的通知的角度来看,您可以使用键值观察(KVO)将其注册为这些模型对象的键路径的观察者。一旦这些属性的值发生变化,您的视图(或中间类)将收到一条消息,然后它可以使用该消息来更新显示。
只要发送消息的类符合这些属性的键值编码 (KVC),这些消息就会发送到注册的观察者。在大多数情况下,通过属性访问您的 ivars 就足够了。
添加观察者:
要收到更改通知:
查看键值观察文档:
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol /Reference/Reference.html#//apple_ref/doc/uid/20002299-SW7
i presume you know or will be able to figure out how to get the activity indicator working.
you can use NSXMLparser delegate methods to parse the xml data. you could collect the xml data in model objects that you make as properties in your class that parses the xml, i.e. a simple array. then, from the view that you want to be notified of the completed parsing, you can register it as an observer for the keypath to those model objects using key value observing (KVO). Once the value of those properties change, your view (or intermediary class) will be sent a message which it can then use to update the display.
These messages will get sent to the registered observers as long as the class sending the message is key value coding (KVC) compliant for those properties. making your ivars accessible through properties is enough in most cases.
to add an observer:
to be notified of changes:
check out the key value observing documentation:
http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html#//apple_ref/doc/uid/20002299-SW7
我认为你应该在指定的时间内使用 NSTimer 类。
通过它执行解析数据的方法(即 GetXMLData --用户定义的方法)方法,
使用停止它启动活动指示器
并在下面的方法中
我希望您明白我在说什么...
如果需要任何帮助请发表评论...
i think you should have to use NSTimer class with specified amount of time.
execute the method which parse the data(i.e. GetXMLData --user defined method) method through it,
and start activity indicator using
stop it in the method below
i hope you undrstand what am i telling...
do comment if need any help...