iOS Swift NSNotificationCenter 通知

来源:互联网 发布:软件开发招标书 编辑:程序博客网 时间:2024/06/05 07:26

1. NSNotification : 一个对象状态变化时通过NSNotification通知另一个或多个对象 (观察者模式 Observer);

2. 发送通知:


  let dic =myvalue 

  NSNotificationCenter.defaultCenter().postNotificationName("Name", object: dic)


3. 接收通知:


  NSNotificationCenter.defaultCenter().addObserver(self, selector: "Myfunc:", name: "Name", object: nil)

  func Myfunc(notification:NSNotification) {

        

       let getStr = notification.objectas! String //这里以通知内容为字符串类型为例

        print("收到通知"+getStr)

    }


4.移除通知:


  deinit{

        NSNotificationCenter.defaultCenter().removeObserver(self)

        }


/*

持续完善更新中...

*/



           


1 0
原创粉丝点击