How to set a delegate in a different class

那年仲夏 提交于 2020-01-01 09:58:27

问题


I'm working with NSXMLParser that parses a xml document. You have to set the delegate which we would be called every time the parser finds an element. The examples I've looked at all set the delegate to be the same class that's createing:

NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:filename];
[parser setDelegate: self];

Other examples set the delegate to be the parent. What if I want another class (not related to the same class) to handle the delegate. What is the syntax to do so?

I've done this but it doesn't work.

@interface Util : NSObject <NSXMLParserDelegate> {
    //Some code here
}

//functions for the delegate and the implementation on the Util.m
//.
//.
//.

Thx for your answers.

I forgot to say that when calling the delegate I assumed that it would be something like this:

[parser setDelegate:Util];

I assumed this knowing that to set the delegate for the same class the message is:

[parser setDelegate:self];

回答1:


You have to create the Util object first.

The delegate has to be an actual instance of a class :)

Util* util = [[Util alloc] init];
[parser setDelegate:util];
[util release];


来源:https://stackoverflow.com/questions/3924484/how-to-set-a-delegate-in-a-different-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!