views:

52

answers:

1

Hello!

The thing is I'm new on iPhone and Objective C development and I'd like to know what's the best way to call some function once you finished a task.

In my application what I do is load an internet hosted XML with some vars. To do this I have created this function called by the main view:

ApplicationVariablesLoader *loader = [ [ ApplicationVariablesLoader alloc] initWithURL:CONFIG_URL ];

where CONFIG_URL is a constant with a valid NSString ( the XML url )

This class parses the XML and extracts the variables, but the thing is, how can I tell the view this process is finished and can continue?

My first idea is to create an instance of the view as a delegate inside the parser class ( ApplicationVariablesLoader ) but I'd like to follow best practices.

Thanks!

A: 

The correct way would be to create a protocol called ApplicationVariablesLoaderDelegate with a (maybe optional) method like - (void)variablesLoaderDidFinishParsing:(ApplicationVariablesLoader *)variablesLoader, and then add something like a

@property (nonatomic, assign) id< ApplicationVariablesLoaderDelegate > delegate;

with the corresponding member variable to your implementation of ApplicationVariablesLoader. The ApplicationVariablesLoader should then upon completion check for [self.delegate respondsToSelector:@selector(variablesLoaderDidFinishParsing:)], and if it's implemented, call [self.delegate variablesLoaderDidFinishParsing:self].

Your view should implement variablesLoaderDidFinishParsing and do whatever you want it to there.

Have a look at UIWebViewDelegate, UITextFieldDelegate, NSXMLParserDelegate etc. for more examples.

MrMage
I found this link interesting too: http://iphoneinaction.manning.com/iphone_in_action/2009/04/create-delegate-protocols-for-the-iphone.html
Xavi Colomer