views:

227

answers:

4

I am new to iphone development.I created a application , in which the first tab bar view ,load 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.Please help me out.Thanks.

+1  A: 

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.

unforgiven
A: 

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

Remover
A: 

Hi,Warrior

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...

yakub_moriss
A: 
  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.
iwat