通知中心和代理

来源:互联网 发布:数据分析 考研专业 编辑:程序博客网 时间:2024/05/16 01:03

  整理一下代理和通知中心的 基本用法。



1,代理,通常用于 有关联的页面之间赋值操作,像PUSH 父与子界面。


A   父视图  。


#import "B 子视图.h"


<mydelegate>



B  子视图。

@protocol mydelegate <NSObject>


-(void)gaibianyan ; //A 中的方法


@end


@property(strong,nonatomic)id<mydelegate> delegate;



在B子视图中不需要去 import  A。



2 通知中心 


 //通知中心注册

    

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(tongzhizhongxin:)name:@"tongzhizhongxin"object:nil];


-(void)tongzhizhongxin:(NSNotification*)notification

{


    NSLog(@"收到通知");

    

    if ([notification.object isEqualToString:@"1"]) {

        NSLog(@"1");

    }

    else

    {

        NSLog(@"2");

    }

    

    

  //发送通知

    [[NSNotificationCenterdefaultCenter] postNotificationName:@"tongzhizhongxin"object:@"1"];

    

    


}




0 0
原创粉丝点击