developerWab 2013-04-07 06:53 采纳率: 0%
浏览 2054
已采纳

NSnotification 传递问题

有一个这样的方法:

-(void)didLoginWithAccount(MyAccount *)account

给这个方法添加了observer :

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginWithAccount:)];

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

  • 写回答

2条回答 默认 最新

  • ReyZhang 移动开发领域新星创作者 2013-04-08 12:51
    关注

    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....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧