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!
The correct way would be to create a protocol called
ApplicationVariablesLoaderDelegatewith 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. TheApplicationVariablesLoadershould then upon completion check for[self.delegate respondsToSelector:@selector(variablesLoaderDidFinishParsing:)], and if it’s implemented, call[self.delegate variablesLoaderDidFinishParsing:self].Your view should implement
variablesLoaderDidFinishParsingand do whatever you want it to there.Have a look at
UIWebViewDelegate,UITextFieldDelegate,NSXMLParserDelegateetc. for more examples.