单知识点运用---使用通知--进行关于不同视图中数值传递

来源:互联网 发布:放烟花网络意思 编辑:程序博客网 时间:2024/05/23 19:12

对事件进行处理有三种方法:

1:忘记了

2:代理

3:通知

使用案例就是76天-项目实战--宾果社区 中的applegate.m下的接收消息方法中接收到的消息内容传递到chatviewcontroller.m中。

applegate.m中

//接收消息

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message

{

    //获得发送信息的用户名

    XMPPJID * jid = [messagefrom];


    //获得发送过来的body

    NSString * body = [[messageelementForName:@"body"]stringValue];

    

    //合并发送人和发送内容

    body = [NSStringstringWithFormat:@"%@: %@", [jiduser], body];

    //创建一个通知

    NSNotification * note = [[NSNotificationalloc]initWithName:@"COMEMSG"object:body userInfo:nil];

    

    //发通知往通知中心投递通知

    [[NSNotificationCenterdefaultCenter]postNotification:note];

    

}


chatviewcontroller.m中


这里涉及到两个方法:

一个是在- (void)viewDidLoad 中:需要注册通知从而得到通知池中已经放入的通知,具体如下:


- (void)viewDidLoad {

    [superviewDidLoad];

    

    //注册通知,这样就可以得到通知池中已经放入的通知

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

    }


之后实现通知的方法

//处理得到的通知

-(void)comeMessage:(id)_o

{

    NSLog(@"%@", [_oobject]);

}






0 0