创建一个对象作为委托- Objective C。

时间:2022-02-16 20:07:13

My question is simple actually, how do I create an object to act as a delegate, instead of including the delegate methods in my view?

我的问题其实很简单,如何创建一个对象作为委托,而不是在视图中包含委托方法?

For example, I have x functionality that requires delegate methods, and they're currently setup to use self as the delegate. I'd like to put those methods in their own object so that the delegate methods can be called and do stuff if the view has ended.

例如,我有需要委托方法的x功能,它们目前正在设置使用self作为委托。我希望将这些方法放到它们自己的对象中,这样当视图结束时,可以调用委托方法并进行处理。

What's the best way?

最好的方法是什么?

2 个解决方案

#1


2  

for example, NSXMLParser delegate methods - they exist, the delegate is defined, but I dont want to call them as self in my view controller... what other option do I have?

例如,NSXMLParser委托方法——它们是存在的,委托是定义的,但是我不想在我的视图控制器中将它们调用为self……我还有什么选择?

You can specify another custom class to handle the delegate methods, if you wish. Simply create a class, call it MyXMLParserDelegate or something similar. Then, all you have to do is tell your NSXMLParser object that it should use an instance of your class as its delegate.

如果愿意,可以指定另一个自定义类来处理委托方法。只需创建一个类,将其命名为MyXMLParserDelegate或类似的东西。然后,只需告诉NSXMLParser对象,它应该使用类的实例作为它的委托。

If you are using Interface Builder, add a new object to the XIB file, set its class to MyXMLParserDelegate, and then drag a connection from your NSXMLParser object's delegate selector to the new object.

如果您正在使用接口构建器,将一个新对象添加到XIB文件,将它的类设置为MyXMLParserDelegate,然后从NSXMLParser对象的委托选择器中拖拉一个连接到新对象。

If you are doing it programmatically, the basic operation looks like this:

如果以编程方式进行,基本操作如下:

MyXMLParserDelegate * myDelegate = [[MyXMLParserDelegate alloc] init];
[someXMLParser setDelegate:myDelegate];

Keep in mind, however, that delegates are not retained, so in order to do this without leaking memory, you should add an ivar of type MyXMLParserDelegate to your viewController class, and then do the following:

但是要记住,委托并没有被保留,所以为了在不泄漏内存的情况下实现这一点,您应该向您的viewController类中添加一个MyXMLParserDelegate类型的ivar,然后执行以下操作:

// in your @interface block:
{
    ...
    MyXMLParserDelegate * myDelegate;
}

// in your init method:
myDelegate = [[MyXMLParserDelegate alloc] init];

// in your awakeFromNib method (or anywhere else it seems appropriate):
[someXMLParser setDelegate:myDelegate];

// in your dealloc method:
[myDelegate release];

#2


1  

Check out this answer, I think it covers what you need: How to use custom delegates in Objective-C

看看这个答案,我认为它涵盖了您需要的内容:如何在Objective-C中使用自定义委托

#1


2  

for example, NSXMLParser delegate methods - they exist, the delegate is defined, but I dont want to call them as self in my view controller... what other option do I have?

例如,NSXMLParser委托方法——它们是存在的,委托是定义的,但是我不想在我的视图控制器中将它们调用为self……我还有什么选择?

You can specify another custom class to handle the delegate methods, if you wish. Simply create a class, call it MyXMLParserDelegate or something similar. Then, all you have to do is tell your NSXMLParser object that it should use an instance of your class as its delegate.

如果愿意,可以指定另一个自定义类来处理委托方法。只需创建一个类,将其命名为MyXMLParserDelegate或类似的东西。然后,只需告诉NSXMLParser对象,它应该使用类的实例作为它的委托。

If you are using Interface Builder, add a new object to the XIB file, set its class to MyXMLParserDelegate, and then drag a connection from your NSXMLParser object's delegate selector to the new object.

如果您正在使用接口构建器,将一个新对象添加到XIB文件,将它的类设置为MyXMLParserDelegate,然后从NSXMLParser对象的委托选择器中拖拉一个连接到新对象。

If you are doing it programmatically, the basic operation looks like this:

如果以编程方式进行,基本操作如下:

MyXMLParserDelegate * myDelegate = [[MyXMLParserDelegate alloc] init];
[someXMLParser setDelegate:myDelegate];

Keep in mind, however, that delegates are not retained, so in order to do this without leaking memory, you should add an ivar of type MyXMLParserDelegate to your viewController class, and then do the following:

但是要记住,委托并没有被保留,所以为了在不泄漏内存的情况下实现这一点,您应该向您的viewController类中添加一个MyXMLParserDelegate类型的ivar,然后执行以下操作:

// in your @interface block:
{
    ...
    MyXMLParserDelegate * myDelegate;
}

// in your init method:
myDelegate = [[MyXMLParserDelegate alloc] init];

// in your awakeFromNib method (or anywhere else it seems appropriate):
[someXMLParser setDelegate:myDelegate];

// in your dealloc method:
[myDelegate release];

#2


1  

Check out this answer, I think it covers what you need: How to use custom delegates in Objective-C

看看这个答案,我认为它涵盖了您需要的内容:如何在Objective-C中使用自定义委托