objective-c - 如何在不同的类中设置委托(delegate)

标签 objective-c delegates nsxmlparser

我正在使用解析 xml 文档的 NSXMLParser。您必须设置每次解析器找到元素时我们都会调用的委托(delegate)。我看过的所有示例都将委托(delegate)设置为正在创建的同一个类:

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

其他示例将委托(delegate)设置为父级。如果我想要另一个类(与同一个类无关)来处理委托(delegate)怎么办。这样做的语法是什么?

我已经这样做了,但它不起作用。

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

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

感谢您的回答。

我忘了说,在调用委托(delegate)时我假设它会是这样的:

[parser setDelegate:Util];

我假设这知道要为同一个类设置委托(delegate),消息是:

[parser setDelegate:self];

最佳答案

您必须先创建 Util 对象。

委托(delegate)必须是类的实际实例:)

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

关于objective-c - 如何在不同的类中设置委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3924484/

相关文章:

ios - XMPPStream Connect 似乎可以工作,但委托(delegate)方法 xmppStreamDidConnect 没有被调用,为什么?

iPhone NSXMLParser(错误 9)

objective-c - 从 NSXMLElement 获取属性的对应值

ios - 使用 NSXMLParser 解析 XML 并获取特定属性值

ios - 应用程序崩溃显示 "UITableView was deallocated while key value observers were still registered with it"

c# - 编译委托(delegate)和调度程序时出错

objective-c - 在基于 View 的 NSTableView 上更改选择颜色

events - Unity3D中的帧率独立事件

iphone - 如何检查用户是否清除了iPhone后台应用程序的运行状态?

ios - 如何在 iOS 的 tableview 中扩展部分?