Delegate 模式 与通知使用介绍

来源:互联网 发布:沈阳淘宝客服招聘信息 编辑:程序博客网 时间:2024/06/03 17:40

//注册一个代理- (id)init { self = [super init]; if (!self) { return nil; }[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(decrementActivityCount) name:@“唯一的字符串可调用通知” object:nil]; return self;}// 发送通知后接受到信息后处理的信息- (void)decrementActivityCount {    }//发送通知[[NSNotificationCenter defaultCenter] postNotificationName:@“唯一的字符串” object:self];//发送与注册要是唯一相同的字符串名称//销毁代理- (void)dealloc {    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"唯一的字符串" object:nil];    [super dealloc];}


定义一个协议#import <UIKit/UIKit.h>@protocol LLBTDelegate <NSObject>- (void)getDelegate;@end@interface LLBTController : UIViewController@property (nonatomic, assign) id<LLBTDelegate>delegate;@end第二个控制器 代理.h 文件 首先导入头文件 在加入协议名称#import <UIKit/UIKit.h>#import "LLBTController.h"@interface LLBTBController : UIViewController<LLBTDelegate>@end.m 文件实现代理实现操作#import "LLBTBController.h"@implementation LLBTBController//代理方法- (void)getDelegate{    NSLog(@"--------");}- (void)viewDidLoad{    [self getDelegate];}@end




原创粉丝点击