这里我写的是个传值的例子,介绍通知,跟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