iPhone中如何让解析完成后出现tabbar视图?

发布于 2024-08-28 05:14:48 字数 174 浏览 9 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(4

深居我梦 2024-09-04 05:14:48

如果没有额外的信息很难判断,但我猜您正在 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() or loadView(). 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: and performSelectorOnMainThread: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.

国产ˉ祖宗 2024-09-04 05:14:48

我想您知道或能够弄清楚如何让活动指示器正常工作。

您可以使用 NSXMLparser 委托方法来解析 xml 数据。您可以在您创建的模型对象中收集 xml 数据,作为解析 xml 的类中的属性,即一个简单的数组。然后,从您希望收到解析完成的通知的角度来看,您可以使用键值观察(KVO)将其注册为这些模型对象的键路径的观察者。一旦这些属性的值发生变化,您的视图(或中间类)将收到一条消息,然后它可以使用该消息来更新显示。

只要发送消息的类符合这些属性的键值编码 (KVC),这些消息就会发送到注册的观察者。在大多数情况下,通过属性访问您的 ivars 就足够了。

添加观察者:

- (void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context

要收到更改通知:

- (void)didChangeValueForKey:(NSString *)key

查看键值观察文档:

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:

- (void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context

to be notified of changes:

- (void)didChangeValueForKey:(NSString *)key

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

荒人说梦 2024-09-04 05:14:48

我认为你应该在指定的时间内使用 NSTimer 类。

通过它执行解析数据的方法(即 GetXMLData --用户定义的方法)方法,

NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(GetXMLData:) userInfo:nil repeats:NO];

使用停止它启动活动指示器

[activityIndicator startAnimating];

并在下面的方法中

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
.
.
.
[activityIndicator stopAnimating];

}

我希望您明白我在说什么...
如果需要任何帮助请发表评论...

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,

NSTimer *currentTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(GetXMLData:) userInfo:nil repeats:NO];

and start activity indicator using

[activityIndicator startAnimating];

stop it in the method below

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
.
.
.
[activityIndicator stopAnimating];

}

i hope you undrstand what am i telling...
do comment if need any help...

紧拥背影 2024-09-04 05:14:48
  1. 不要在 MainThread 中进行解析,在
  2. 解析(parserDidEndDocument:)后分离一个新线程进行解析([NSThread detachNewThreadSelector:toTarget:withObject:])使用 performSelectorOnMainThread刷新tableView。
  1. don't do parsing in the MainThread, detach a new thread to do parsing ([NSThread detachNewThreadSelector:toTarget:withObject:])
  2. after parsed (parserDidEndDocument:) use performSelectorOnMainThread to refresh the tableView.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文