IOS Local Notifications

Source: Internet
Author: User

Learning iOS also has a period of time, notice has not seen, today learned a bit;

Notifications are primarily used for data sharing and thread communication between different objects (these are professional phrases that I don't quite understand, figuring out what to do and when to use it).

I watched the wireless interconnect video about local notifications (there's only one simple example), but it's right for my appetite.

Example Application scenario:

A child class in which there is a Nsinteger property initialized to 100, a timer is defined to let this attribute value minus 2 per two seconds, and when it is reduced to 90, a notification is sent to the notification center.

A father class, registered in the class to receive this notification, when the child sends a notification father to receive the notification and to respond accordingly.

@interface Child:nsobject@property (nonatomic,assign) nsinteger  num; @end

#import "Child.h" @implementation child@synthesize num;-(id) init{self    = [super init];    if (self) {        num = +;        [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeobserver:) Userinfo:nil repeats:yes];    }    return self;} -(void) Timeobserver: (Nstimer *) timer{    NSLog (@ "child:%ld", num);    num-= 2;    if (num = =) {        [[Nsnotificationcenter defaultcenter]postnotificationname:@ "Child" Object:[nsnumber numberwithinteger:num]];//The first parameter is the name of the notification, is used to identify the notification, the second argument object is the passed argument            }} @end

@implementation father-(ID) init{self    = [super init];    if (self) {        [[nsnotificationcenter defaultcenter]addobserver:self selector: @selector (Observer:) name:@ ' child ' object:nil];//Register to receive the notification, <pre name= "code" class= "OBJC" > if the fourth parameter is Nil<span style= "font-family:arial, Helvetica, Sans-serif; " > represents the receipt of a notification named name (the third parameter), and if the fourth parameter specifies an object, only the parameter named name under that object is received. </span>
} return self;} -(void) Observer: (Nsnotification *) notification{Nsinteger i = (nsinteger) notification.object; NSLog (@ "%ld", (long) i);} @end



IOS Local Notifications

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.