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