如何在Objective-C中建立委托?

我知道表明们的工做方式,我知道如何使用它们。 程序员

可是我该如何建立呢? 函数


#1楼

批准的答案很棒,但若是您正在寻找1分钟的答案,请尝试如下方法: post

MyClass.h文件应以下所示(添加带注释的委托行!) this

#import <BlaClass/BlaClass.h>

@class MyClass;             //define class, so protocol can see MyClass
@protocol MyClassDelegate <NSObject>   //define delegate protocol
    - (void) myClassDelegateMethod: (MyClass *) sender;  //define delegate method to be implemented within another class
@end //end protocol

@interface MyClass : NSObject {
}
@property (nonatomic, weak) id <MyClassDelegate> delegate; //define MyClassDelegate as delegate

@end

MyClass.m文件应以下所示 atom

#import "MyClass.h"
@implementation MyClass 
@synthesize delegate; //synthesise  MyClassDelegate delegate

- (void) myMethodToDoStuff {
    [self.delegate myClassDelegateMethod:self]; //this will call the method implemented in your other class    
}

@end

要在另外一个类中使用您的委托(在本例中称为MyVC的UIViewController)MyVC.h: spa

#import "MyClass.h"
@interface MyVC:UIViewController <MyClassDelegate> { //make it a delegate for MyClassDelegate
}

MyVC.m: 指针

myClass.delegate = self;          //set its delegate to self somewhere

实现委托方法 code

- (void) myClassDelegateMethod: (MyClass *) sender {
    NSLog(@"Delegates are great!");
}

#2楼

请! 查看如下简单的分步教程,了解Delegates如何在iOS中工做。 orm

在iOS中表明 server

我建立了两个ViewControllers(用于将数据从一个发送到另外一个)

  1. FirstViewController实现委托(提供数据)。
  2. SecondViewController声明委托(将接收数据)。

#3楼

我认为,一旦你理解了表明,全部这些答案都颇有意义。 就我的而言,我来自C / C ++以前,在此以前的程序语言如Fortran等,因此这是我在C ++范例中寻找相似相似物的2分钟。

若是我要向C ++ / Java程序员解释表明,我会说

表明们是什么? 这些是指向另外一个类中的类的静态指针。 分配指针后,能够调用该类中的函数/方法。 所以,您的类的某些函数被“委托”(在C ++世界中 - 由类对象指针指向)到另外一个类。

什么是协议? 从概念上讲,它的做用与您指定为委托类的类的头文件相似。 协议是一种明确的方法,用于定义须要在类中实现哪些方法的指针被设置为类中的委托。

我怎样才能在C ++中作相似的事情? 若是您尝试在C ++中执行此操做,则能够经过在类定义中定义指向类(对象)的指针,而后将它们链接到其余类,这些类将提供其余函数做为基类的委托。 可是这种布线须要在代码中保留,而且会很笨拙且容易出错。 Objective C假设程序员不擅长维护这个decipline并提供编译器限制来强制执行干净的实现。


#4楼

好吧,这不是问题的真正答案,可是若是你正在查找如何制做本身的表明,那么更简单的东西多是更好的答案。

我很难实现个人表明,由于我不多须要。 我只能拥有一个委托对象的委托。 所以,若是您但愿您的表明以单向方式进行通讯/传递数据,而不是经过通知更好。

NSNotification能够将对象传递给多个收件人,而且它很是易于使用。 它的工做原理以下:

MyClass.m文件应以下所示

#import "MyClass.h"
@implementation MyClass 

- (void) myMethodToDoStuff {
//this will post a notification with myClassData (NSArray in this case)  in its userInfo dict and self as an object
[[NSNotificationCenter defaultCenter] postNotificationName:@"myClassUpdatedData"
                                                    object:self
                                                  userInfo:[NSDictionary dictionaryWithObject:selectedLocation[@"myClassData"] forKey:@"myClassData"]];
}
@end

要在另外一个类中使用您的通知:将类添加为观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(otherClassUpdatedItsData:) name:@"myClassUpdatedData" object:nil];

实现选择器:

- (void) otherClassUpdatedItsData:(NSNotification *)note {
    NSLog(@"*** Other class updated its data ***");
    MyClass *otherClass = [note object];  //the object itself, you can call back any selector if you want
    NSArray *otherClassData = [note userInfo][@"myClassData"]; //get myClass data object and do whatever you want with it
}

若是是,请不要忘记以观察者的身份删除您的班级

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#5楼

做为Apple推荐的一种好的作法,表明(根据定义,它是一个协议)符合NSObject协议是有益的。

@protocol MyDelegate <NSObject>
    ...
@end

&在您的委托中建立可选方法(即不必定须要实现的方法),您可使用@optional注释,以下所示:

@protocol MyDelegate <NSObject>
    ...
    ...
      // Declaration for Methods that 'must' be implemented'
    ...
    ...
    @optional
    ...
      // Declaration for Methods that 'need not necessarily' be implemented by the class conforming to your delegate
    ...
@end

所以,当您使用已指定为可选的方法时,您须要(在您的类中)检查respondsToSelector若是视图(符合您的委托)实际上已实现您的可选方法。

相关文章
相关标签/搜索