您的位置:首页 > 移动开发 > Swift

Swift 通知的使用(十一)

2016-04-02 01:06 267 查看

这里我写的是个传值的例子,介绍通知,跟OC的使用基本差不多

let center = NSNotificationCenter.defaultCenter()
center.addObserver(self, selector: "getValue:", name: "name", object: nil)


func getValue(object: NSNotification) ->Void{
print(object.object)
print("object")
}


func click()->Void{
let dic = NSMutableDictionary()
dic.setValue("str", forKey: "name")
print(dic)
NSNotificationCenter.defaultCenter().postNotificationName("name", object: dic as [NSObject: AnyObject])
self.dismissViewControllerAnimated(true) { () -> Void in
//
}

}


使用之后记得移除

deinit{
// 移除通知
//        NSNotificationCenter.defaultCenter().removeObserver(self, name: "name", object: nil)
//        NSNotificationCenter.defaultCenter().removeObserver(self)
}


Swift / OC 进阶群: 498143780
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: