如何在不同的类中设置一个委托 [英] How to set a delegate in a different class

查看:145
本文介绍了如何在不同的类中设置一个委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用解析XML文档的NSXMLParser。每次解析器找到一个元素时,都必须设置我们将被调用的委托。我看过的例子都将代理设置成与创建的同一个类:

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

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



我已经完成了这项工作,但无效。

  @interface Util:NSObject&NSMMLParserDelegate> {
//这里的一些代码
}

//在Util.m
//上的委托和实现的函数。
//。
//。

Thx为您的答案。



忘了说当我打电话给代理人时,我认为这将是这样的:

  [parser setDelegate:Util]; 

我认为要知道为同一个类设置委托消息是:

  [parser setDelegate:self]; 


解决方案

你必须先创建Util对象。 p>

代理必须是一个类的实际实例:)

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


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];

解决方案

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];

这篇关于如何在不同的类中设置一个委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆