从类实现中调用协议中声明的方法

时间:2010-12-27 17:06:16

标签: iphone objective-c protocols

我创建了一个名为 RecDelegate 的协议,该协议由一个方法“ - (void)doSmtng ”组成。 协议在rec接口声明之前的 rec.h 中定义 当我创建一个新应用程序并采用 RecDelegate 到新的 appDelegate 时,我可以根据需要实现自己的doSmtng。
我不明白的是如何从rec.m调用doSmtng方法(定义协议的类的实现......) - 意思是,我如何以这样一种方式“触发”doSmtng将触发新的appDelegate的实现。

希望我所说的相对清楚......;)

谢谢, 盖

3 个答案:

答案 0 :(得分:2)

下面有两个控制器,显示如何从一个触发事件到另一个事件。 只要有“// HERE”注释,就表示存在与委托相关的代码。

代表发送

SecondViewController.h

#import <UIKit/UIKit.h> 

@protocol SecondDelegate <NSObject> // HERE
  @optional
    -(void)MessageReceived:(NSString *)msg;
@end


@interface SecondViewController : UIViewController {
    id<SecondDelegate> secondDelegate; // HERE
}

@property (assign) id<SecondDelegate> secondDelegate; // HERE

-(IBAction)trigger:(id)sender;

@end

SecondViewController.m

#import "SecondViewController.h" 

@implementation SecondViewController

@synthesize secondDelegate; // HERE

-(IBAction)trigger:(id)sender {
    if (self.secondDelegate != NULL && [self.secondDelegate  respondsToSelector:@selector(MessageReceived:)]) { // HERE
        [secondDelegate MessageReceived:@"my message"]; 
    }
}

接收代表

FirstViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h" // HERE

@interface FirstViewController : UINavigationController <SecondDelegate> // HERE

-(void)MessageReceived:(NSString*)msg; // HERE

@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
        [self.view setBackgroundColor:[UIColor greenColor]];
        SecondViewController *second = [[SecondViewController alloc] init];
        [self pushViewController:second animated:YES];
        second.secondDelegate = self; // HERE
        [second release];
    }
    return self;
}

-(void)MessageReceived:(NSString *)msg { // HERE
    int y = 0; // HERE IT IS !
}

答案 1 :(得分:1)

您需要告诉Rec对象它应该将您的AppDelegate视为其代理人:

[rec setDelegate:appDelegate];

这可以通过Interface Builder完成,也可以在创建Rec对象后完成。

然后,当Rec对象将委托消息发送给其委托时,接收方将是您的AppDelegate实例:

[[self delegate] doSmtng];

如果Rec对象发送给其委托的消息是可选的协议消息,则它将像这样发送:

if ([[self delegate] respondsToSelector:@selector(optionalProtocolMethod)]) {
    [[self delegate] optionalProtocolMethod];
}

delegate通常会被声明为:

@property(assign, nonatomic) id<RecDelegate> delegate;

由于未保留-deallocRec对象只需要nil,而不是释放它:

delegate = nil;

答案 2 :(得分:0)

执行类似操作的另一种方法是使用NSNotificationCenter

在RecDelegate init方法中添加:

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

以及任何其他地方/任何课堂电话

[[NSNotificationCenter defaultCenter] postNotificationName:@"someNotification" object:nil];