Swift 通知的使用(十一)

来源:互联网 发布:影楼调色软件 编辑:程序博客网 时间:2024/06/05 04:23

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

3 0