IOS local notification

Source: Internet
Author: User

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



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.