如何在 Objective-C 中使用自定义委托

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/645449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:15:13  来源:igfitidea点击:

How to use custom delegates in Objective-C

objective-ccocoa-touchdelegates

提问by Sreelal

I need to know about the usage of delegate methods in Objective-C. Can anyone point me to the correct source?

我需要了解 Objective-C 中委托方法的使用。谁能指出我正确的来源?

回答by Jonathan Sterling

You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class Foomight look like this:

您将需要为您的类声明一个委托协议。类的委托协议和接口的示例Foo可能如下所示:

@class Foo;
@protocol FooDelegate <NSObject>
@optional
- (BOOL)foo:(Foo *)foo willDoSomethingAnimated:(BOOL)flag;
- (void)foo:(Foo *)foo didDoSomethingAnimated:(BOOL)flag;
@end

@interface Foo : NSObject {
     NSString *bar;
     id <FooDelegate> delegate;
}

@property (nonatomic, retain) NSString *bar;
@property (nonatomic, assign) id <FooDelegate> delegate;

- (void)someAction;

@end

Don't forget to synthesize your properties in the @implementation.

不要忘记在@implementation.

What this code did was declare a protocol called FooDelegate; a class that conforms to this protocol would be declared like @interface SomeClass : SuperClass <FooDelegate> {}. Because this class conforms to the protocol FooDelegate, it now gets to implement the methods under FooDelegate(to require that these be implemented, use @requiredinstead of @optional). The last step is for a Fooobject to be instantiated in the class that conforms to FooDelegate, and for this Fooobject to have its delegate property set:

这段代码所做的是声明一个名为 FooDelegate 的协议;符合此协议的类将被声明为@interface SomeClass : SuperClass <FooDelegate> {}。因为这个类符合协议FooDelegate,它现在可以实现下面的方法FooDelegate(要求实现这些方法,使用@required代替@optional)。最后一步是Foo在符合 的类中实例化一个对象FooDelegate,并为该Foo对象设置其委托属性:

Foo *obj = [[Foo alloc] init];
[obj setDelegate:self];

Now, your class is prepared to receive messages from Fooobjects that have their delegates set correctly.

现在,您的类已准备好接收来自Foo已正确设置其委托的对象的消息。

回答by iCrazyDev

Delegates are very useful to control transfer within the array of view controllers in app manually. Using delegates you can manage the control flow very well.

委托对于手动控制应用程序中视图控制器数组内的传输非常有用。使用委托可以很好地管理控制流。

here is small example of own delegates....

这是自己的代表的小例子......

  1. Create a protocol class.... (.h only)
  1. 创建一个协议类....(仅.h)

SampleDelegate.h

SampleDelegate.h

#import
@protocol SampleDelegate
@optional

#pragma Home Delegate

-(NSString *)getViewName;

@end
  1. Import above protocol class in the class whom you want to make delegate of another class. Here in my ex. I m using AppDelegate to make delegate of The HomeViewController's Object.
  1. 在要委托另一个类的类中导入上述协议类。在我的前任。我正在使用 AppDelegate 来委托 HomeViewController 的对象。

also add above DelegateName in Delegate Reference < >

还要在 Delegate Reference < > 中添加上面的 DelegateName

ownDelegateAppDelegate.h

ownDelegateAppDelegate.h

#import "SampleDelegate.h"

@interface ownDelegateAppDelegate : NSObject <UIApplicationDelegate, SampleDelegate>
{


}

ownDelegateAppDelegate.m

ownDelegateAppDelegate.m

//setDelegate of the HomeViewController's object as
[homeViewControllerObject setDelegate:self];

//add this delegate method definition
-(NSString *)getViewName
{
    return @"Delegate Called";
}

HomeViewController.h

主视图控制器.h

#import
#import "SampleDelegate.h"

@interface HomeViewController : UIViewController 
{

    id<SampleDelegate>delegate;
}

@property(readwrite , assign) id<SampleDelegate>delegate;

@end

HomeViewController.h

主视图控制器.h

- (void)viewDidAppear:(BOOL)animated 
{

    [super viewDidAppear:animated];
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    lblTitle.text = [delegate getViewName];
    lblTitle.textAlignment = UITextAlignmentCenter;
    [self.view addSubview:lblTitle];

}

回答by Sean Patrick Murphy

To start, you can take a look at what Apple has to say about delegate methods. The documentation provides some well written information about what delegation is all about, and explains both how to use AppKit classes that define and support a delegate and how to code delegate support into one of your own objects.

首先,您可以看看 Apple 对委托方法的看法。该文档提供了一些关于委托的详细信息,并解释了如何使用定义和支持委托的 AppKit 类以及如何将委托支持编码到您自己的对象之一中。

See Communicating With Objects

请参阅与对象通信

(If you're interested in coding your own delegate support, skip down to the "Implementing a Delegate for a Custom Class" section.)

(如果您对编写自己的委托支持感兴趣,请跳到“为自定义类实现委托”部分。)

The most significant aspect to take away from delegate methods is that they enable you to customize and affect the behavior of an object without the need to subclass it.

摆脱委托方法的最重要方面是它们使您能够自定义和影响对象的行为,而无需对其进行子类化。

Hope that helps you get started.

希望能帮助您入门。

回答by dlamblin

If the object(s) in question has its delegateassigned to a class you wrote, say a controllerthen the methods defined for being that object's class's delegatemethods must be implemented by the assigned class. This allows you to effectively control the behavior of the object without sub-classing the object's class in order to override behavior that would likely necessitate an amount of duplicating behavior. It's one of the cleaner parts of the cocoa touch design.

如果有问题的对象已delegate分配给您编写的类,例如 a,controller那么定义为该对象类的delegate方法的方法必须由分配的类实现。这允许您有效地控制对象的行为,而无需对对象的类进行子类化,以覆盖可能需要大量重复行为的行为。它是可可触感设计中更干净的部分之一。

This is something you should pick up in the first couple of intros and tutorials to cocoa touch. Like this tutorial from Cocoa is my Girlfriend. In fact they made the delegateexplanation a big bold heading.

这是您应该在 Cocoa Touch 的前几个介绍和教程中学习的内容。像Cocoa 的这个教程是我的女朋友。事实上,他们把delegate解释变成了一个大胆的标题。