zoukankan      html  css  js  c++  java
  • iPhone Development 101: Cocoa: Notifications

    From: http://www.idev101.com/code/Cocoa/Notifications.html

    Notifications are an incredibly useful way to send messages (and data) between objects that otherwise don't know about each other. Think of it like a radio station broadcasting messages: the station (sender) broadcasts the message, and listeners can choose to listen in and receive some (or all) of the broadcast messages.

    For example, you might have a network reachability check in your app delegate, and post notifications whenever the reachability changes. Other objects would listen for that notification and react accordingly when the network goes up or down.

    Some Cocoa frameworks send notifications when certain events happen. In iOS 4.0 and up, the application sends notifications when an app is about to move the background... and when it returns from the background. Your app's objects and controllers can listen for these notifications and stop actions (or save data) when the app is about to quit, and resume when it becomes active again.

    Posting Notifications

    To post a notification, the sending object calls the following method:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:self];
    

    The notification name can be any NSString. object should be self (the sender), or nil.

    If you need to pass data along with the notification, use the following method and pass an NSDictionary as the userInfo argument:

    BOOL isReachable = YES;
    NSDictionary *dataDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:isReachable] 
                                                         forKey:@"isReachable"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:self userInfo:dataDict];
    

    Listening for Notifications

    To listen for notifications, an object must:

    1. Add itself as on observer. This can go in the object's init method, or the controller's viewDidLoad*.

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(handleReachabilityChange:)        
                                                 name:@"reachabilityChanged" 
                                               object:nil];
    

    The object: argument can either be a specific object to listen for notifications from, or nil to listen for notifications from any object.

    2. Declare and implement the method named in the @selector:

    - (void)handleReachabilityChange:(NSNotification *)note {
        NSDictionary *theData = [note userInfo];
        if (theData != nil) {
            NSNumber *n = [theData objectForKey:@"isReachable"];
            BOOL isReachable = [n boolValue];
            NSLog(@"reachable: %d", isReachable);
        }
    }
    

    The NSNotification object has three key properties you can access: the notification name, the sending object, and the userInfo dictionary that was passed.

    3. Remove itself as an observer before the object is deallocated:

    -(void) dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
    }
    

    Don't forget this part! If you fail to remove the object as an observer, then the notification center will send notifications to your deallocated object, which will crash your app.

    *If your view controller only needs to respond to notifications while the view is visible, you may want to put the addObserver code in viewDidAppear, and removeObserver in viewDidDisappear. This way your controller won't be responding to notifications when it isn't the current view. (This is particularly important if you're using a navigation controller or tab bar controller, where the controller object may exist but its view isn't the current view - or worse, its view has been unloaded.)

    Additional References

    You can also access the documentation in Xcode by typing ⌘/ to launch the documentation window.

  • 相关阅读:
    2.1.1 Speed Limit
    2.1.2 骑自行车的最短时间
    1.3.1提高实数精度的范例
    1.2.2一个数可以有多少种用连续素数之和表示
    求二倍关系的个数 1.2.1
    求平均值
    原生JS 购物车及购物页面的cookie使用
    基于Jquery的商城商品图片的放大镜效果(非组件)
    商城商品购买数量增减的完美JS效果
    弹性布局各种坑爹兼容
  • 原文地址:https://www.cnblogs.com/simonshi2012/p/2715464.html
Copyright © 2011-2022 走看看