天天看點

Sending , Listening for and Reacting to Notifications (發送,監聽通知)

發送方:

//初始化通知内容  senderObject:self   userInfo:接收到通知時可從中擷取到的資訊

+ (instancetype)notificationWithName:(NSString *)aName object:(id)senderObject userInfo:(NSDictionary *)userInfo

//發送通知

- (void)postNotification:(NSNotification *)notification

接收方(觀察者,監聽者):

//注冊通知

- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

//取消通知

- (void)removeObserver:(id)notificationObserver

e.g.

======AppDelegate.h檔案======

#import <UIKit/UIKit.h>

//notificationName

extern NSString *const kSetPersonInfoNotification; 

//The first-name key in the user-info dictionary of our notification 

extern NSString *const kSetPersonInfoKeyFirstName; 

// The last-name key in the user-info dictionary of our notification 

extern NSString *const kSetPersonInfoKeyLastName;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

=======AppDelegate.m檔案=====

#import "AppDelegate.h"

#import "Person.h"

NSString *const kSetPersonInfoNotification = @"SetPersonInfoNotification";

NSString *const kSetPersonInfoKeyFirstName = @"firstName";

NSString *const kSetPersonInfoKeyLastName = @"lastName";

@implementation AppDelegate

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    Person *steveJobs = [[Person alloc] init];   

    NSNotification *notification =

        [NSNotification  notificationWithName:kSetPersonInfoNotification

                                       object:self  //sender

                                     userInfo:@{kSetPersonInfoKeyFirstName : @"Steve",

                                                 kSetPersonInfoKeyLastName : @"Jobs"}];

   [[NSNotificationCenter defaultCenter] postNotification:notification];

    NSLog(@"Person's first name = %@", steveJobs.firstName);

    NSLog(@"Person's last name = %@", steveJobs.lastName);

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

========Person.h=========

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *firstName;

@property (nonatomic, copy) NSString *lastName;

@end

========Person.m=========

#import "Person.h"

#import "AppDelegate.h"

@implementation Person

- (void) handleSetPersonInfoNotification:(NSNotification *)paramNotification{

    self.firstName =paramNotification.userInfo[kSetPersonInfoKeyFirstName];

    self.lastName = paramNotification.userInfo[kSetPersonInfoKeyLastName];   

}

//instancetype:傳回類型  id:作為參數

- (instancetype) init{

    self = [super init];

    if (self != nil){       

       NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

        //注冊通知  addObserver必須要對應一個removeObserver,否則可能會造成記憶體洩漏     

        [center addObserver:self  //觀察者,監聽者

                  selector:@selector(handleSetPersonInfoNotification:) //接收到通知時,執行的方法,必須要有一個NSNotification參數

                      name:kSetPersonInfoNotification   //通知名稱

                    object:[[UIApplication sharedApplication] delegate]];  //發送(通知)者        

    }

    return self;

}

- (void) dealloc{

    //取消注冊

   [[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

繼續閱讀