iOS通知

本文详细介绍如何在iOS开发中使用NSNotification进行跨组件通信,包括创建、发送及接收通知的过程,并提供了具体的代码实现。

通知的思路要清楚:创建通知==>发送通知==>注册通知==>接受通知调用那个函数


第一步:

//创建通知


//接收到通知后要传的参数

     NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:sender.titleLabel.text,@"oneName",[NSString stringWithFormat:@"%ld",(long int)sender.tag],@"oneId",nil];


   //创建通知对象

    NSNotification *notification = [NSNotification notificationWithName:@"shopChangeData" object:nil userInfo:dict];

    //通过通知中心发送通知

    [[NSNotificationCenter defaultCenter] postNotification:notification];



第二步:在你要接受通知的控制器里注册通知的监听

    //监听数据的通知

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



//触发的函数

//接受Data发过来的通知

-(void)noticeData:(NSNotification *)text

{

    //更新数据

    NSLog(@"%@",text.userInfo[@"oneName"]);

    NSLog(@"%@",text.userInfo[@"oneId"]);

    NSLog(@"数据==>HOME");

    [self initWithNav:text.userInfo[@"oneName"]];

}

注:如果第二步通知没有注册是不会接受到通知的,通知会丢弃。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值