IOS local notification
I have been learning iOS for a while, and I have not read the notifications yet. I learned it today;
Notifications are mainly used for data sharing and thread communication between different objects (I don't know much about these professional phrases, so I can understand what to do and when to use them ).
I watched wireless Internet videos about local notifications (only one simple example), but it suits my appetite.
Application scenarios:
A child class has an NSInteger attribute initialized to 100 in the class. It defines a timer to reduce this attribute value by 2 every two seconds. When it is reduced to 90, the notification center wants to send a notification.
A father class registers to receive the notification. When a child sends the notification, father receives the notification and reflects it 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 = 100; [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 = 90) {[[nsicationicationcenter defacenter center] postNotificationName: @" child "object: [NSNumber numberWithInteger: num]; // The first parameter is the name of the notification used to identify the notification. The second parameter object is the passed parameter }}@ end
@ Implementation Father-(id) init {self = [super init]; if (self) {[[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (observer :) name: @ "child" object: nil]; // register to receive notifications,If the fourth parameter is nil, it indicates that the notification with the name (the third parameter) is received. If the fourth parameter specifies an object, only the parameter named name under the object is received.
} Return self;}-(void) observer :( NSNotification *) notification {NSInteger I = (NSInteger) notification. object; NSLog (@ "% ld", (long) I) ;}@ end