ios-Observer Design Pattern

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.