views:

201

answers:

1

I currently have NSXMLParser working in my viewcontroller. I would like to create a new class that will have the 2 data elements as properties and do all of the xmlparsing. The problem I have is getting the new class to "alert" the parent when it is done parsing, etc. So the viewcontroller (the parent) can then turn off the activity indicator and then update the view with the information. like

[myParent jobCompleted];

How do I reference the parent that initialized the object. Can I setup a custom notification for that class so I can create an observer to monitor it?

A: 

In lack of more info I'm going to assume somethings about your implementation..

So I guess you have a delegate (which implements the NSXMLParserDelegate Protocol) for your NSXMLParser doing all the parsing.. Right?

So the delegate protocol specifies a method called:

- (void)parserDidEndDocument:(NSXMLParser *)parser

If you implement that in your delegate you will know when the document has been parsed. So now you need to tell your viewcontroller about this event. Basically there are two ways of doing this. Either you make a "hard" connection between the NSXMLParserDelegate and your viewcontroller. This is basically done by specifying a property for that particular viewcontroller and setting that property to point to the viewcontroller when you've allocated the NSXMLParserDelegate object. And then you will be able to send a message to the "parent" in the parserDidEndDocument delegate method

- (void)parserDidEndDocument:(NSXMLParser *)parser{
    [viewcontroller jobCompleted];
}

The other way is to take advantage of the delegate pattern and specifying a NSXMLParserDelegateDelegate Protocol (or named something else). That could look something like this:

@protocol NSXMLParserDelegateDelegate

-(void)parserDidEndDocument:(NSXMLParserDelegate *)parserDelegate;

@end

and then make a delegate property in your NSXMLParserDelegate

@interface NSXMLParserDelegate : NSObject<NSXMLParserDelegate>{
    id<NSXMLParserDelegateDelegate> _delegate;
}
@property (assign) id<NSXMLParserDelegateDelegate> _delegate;

@end

remember to synthesize it in the .m file.

The latter way of doing it will give you a more lose connection thus making it easier to move the thing around.

Hope it helped.. Else let me know... And if I'm totally wrong here guys - let me know =)

Jakob Dam Jensen
I was trying to essentially hardcode it. So when I create a new object XYZ from inside ABC I could reference ABC from within XYZ
SonnyBurnette