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

时间:2021-09-16 18:52:59

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

我需要知道Objective-C中委托方法的用法。谁能给我指出正确的出处吗?

4 个解决方案

#1


128  

You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class Foo might 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 @required instead of @optional). The last step is for a Foo object to be instantiated in the class that conforms to FooDelegate, and for this Foo object to have its delegate property set:

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

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

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

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

#2


8  

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.

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

here is small example of own delegates....

这是很小的例子的代表....

  1. Create a protocol class.... (.h only)
  2. 创建一个协议类....(。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.
  2. 导入您想要成为另一个类的委托的类中的协议类。在我的ex中,我使用AppDelegate创建HomeViewController的委托。

also add above DelegateName in Delegate Reference < >

还可以在委托引用< >中添加上面的Delegate atename

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

HomeViewController.h

#import
#import "SampleDelegate.h"

@interface HomeViewController : UIViewController 
{

    id<SampleDelegate>delegate;
}

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

@end

HomeViewController.h

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

}

#3


2  

If the object(s) in question has its delegate assigned to a class you wrote, say a controller then the methods defined for being that object's class's delegate methods 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.

如果所讨论的对象的委托被分配给您编写的类,比如一个控制器,那么定义为该对象的类的委托方法的方法必须由所分配的类实现。这允许您有效地控制对象的行为,而无需对对象的类进行子类化,以便覆盖可能需要大量重复行为的行为。这是可可触摸设计中比较干净的部分之一。

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 delegate explanation a big bold heading.

这是您应该在介绍cocoa touch的前几篇介绍和教程中学习的内容。就像这个来自Cocoa的教程是我的女朋友。事实上,他们把委托解释变成了一个大标题。

#4


2  

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.

首先,你可以看看苹果对委托方法的看法。该文档提供了一些关于什么是委托的详细信息,并解释了如何使用定义和支持委托的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.

希望这能帮助你开始。

#1


128  

You will want to declare a delegate protocol for your class. An example of a delegate protocol and interface for class Foo might 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 @required instead of @optional). The last step is for a Foo object to be instantiated in the class that conforms to FooDelegate, and for this Foo object to have its delegate property set:

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

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

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

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

#2


8  

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.

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

here is small example of own delegates....

这是很小的例子的代表....

  1. Create a protocol class.... (.h only)
  2. 创建一个协议类....(。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.
  2. 导入您想要成为另一个类的委托的类中的协议类。在我的ex中,我使用AppDelegate创建HomeViewController的委托。

also add above DelegateName in Delegate Reference < >

还可以在委托引用< >中添加上面的Delegate atename

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

HomeViewController.h

#import
#import "SampleDelegate.h"

@interface HomeViewController : UIViewController 
{

    id<SampleDelegate>delegate;
}

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

@end

HomeViewController.h

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

}

#3


2  

If the object(s) in question has its delegate assigned to a class you wrote, say a controller then the methods defined for being that object's class's delegate methods 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.

如果所讨论的对象的委托被分配给您编写的类,比如一个控制器,那么定义为该对象的类的委托方法的方法必须由所分配的类实现。这允许您有效地控制对象的行为,而无需对对象的类进行子类化,以便覆盖可能需要大量重复行为的行为。这是可可触摸设计中比较干净的部分之一。

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 delegate explanation a big bold heading.

这是您应该在介绍cocoa touch的前几篇介绍和教程中学习的内容。就像这个来自Cocoa的教程是我的女朋友。事实上,他们把委托解释变成了一个大标题。

#4


2  

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.

首先,你可以看看苹果对委托方法的看法。该文档提供了一些关于什么是委托的详细信息,并解释了如何使用定义和支持委托的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.

希望这能帮助你开始。