Nsnotificationcenter.defaultcenter (). Addobserver (< #observer: Anyobject#>, selector: < #Selector #>, name : < #String #>, object: < #AnyObject?#>)
Nsnotificationcenter.defaultcenter (). Addobserver (Self is the Observer, selector: "" Select the method to process the notification (the method to receive the notification), name: "" : Nil object is empty)
The Red callout section below is the wrong part.
Import UIKit
Class Viewcontroller:uiviewcontroller {
@IBAction func OnClick (sender:anyobject) {
var Resviewcontroller = Self.storyboard?. Instantiateviewcontrollerwithidentifier ("Navviewcontroller") as Uinavigationcontroller
Self.presentviewcontroller (Resviewcontroller, Animated:true) {(), Void in
NSLog ("Pop-up Modal View")
}
}
Basically, the method is not preceded by a colon, it is a method to receive the notification, a colon is required, and the string is not represented by a colon.
Override Func Viewdidload () {
Super.viewdidload ()
Nsnotificationcenter.defaultcenter (). Addobserver (Self,
Selector: "Registercompletion:",
Name: "Registercompletionnotification", Object:nil)
Monitoring System Notifications
Nsnotificationcenter.defaultcenter (). Addobserver (Self,
Selector: "Handleenterbackground:",
Name:uiapplicationdidenterbackgroundnotification, Object:nil)
Nsnotificationcenter.defaultcenter (). Addobserver (Self,
Selector: "Handleenterforeground:",
Name:uiapplicationwillenterforegroundnotification, Object:nil)
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Nsnotificationcenter.defaultcenter (). Removeobserver (self)
}
Todo Receive notifications
Func registercompletion (notification:nsnotification) {
Let userinfo:dictionary<string,string!> = Notification.userinfo as dictionary<string,string!>
Let username = userinfo["username"]!
NSLog ("username =%@", username)
}
Func Handleenterbackground (notification:nsnotification) {
NSLog ("Go into the background")
}
Func Handleenterforeground (notification:nsnotification) {
NSLog ("Back to Reception")
}
}
ios-Observer Design Pattern