开发者社区> 问答> 正文

关于NSnotification 传递的问题

有一个这样的方法:
`
-(void)didLoginWithAccount(MyAccount *)account
`

给这个方法添加了observer
`
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginWithAccount:)];
`

问题是发了通知,怎么传递一个MyAccount对象。

展开
收起
爵霸 2016-03-24 09:28:10 1650 0
1 条回答
写回答
取消 提交回答
  • 1.添加观察者:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginWithAccount:) name:@"app:Login" object:nil];

    2.处理方法
    而处理的方法didLoginWithAccount: 它的定义应该如下

    -(void)didLoginWithAccount:(NSNotification*)notification

    3.发送通知,通知观察者

    MyAccount *account=.....;  //得到你的account信息,并传递给观察者
    [[NSNotificationCenter defaultCenter] postNotificationName:@"app:Login" object:account userInfo:nil];

    需要注意的是无论是定义观察者还是发送通知,它们的 NotificationName 必须一致,这是通知中心唯一能关联的方式,以此来判断是哪个发的通知,又有哪个来接收通知。

    在添加观察者的view 中,获取传递过来的account信息

    -(void)didLoginWithAccount:(NSNotification*)notification {
          MyAccount *account=(MyAccount*)[notification object];
          //todo....
    }
    2019-07-17 19:11:54
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载