ios通知使用 书上案例 简单易懂


/* The notification name */
const NSString *ResultOfAppendingTwoStringsNotification =
@"ResultOfAppendingTwoStringsNotification"; /* Keys inside the dictionary that our notification sends */
const NSString
*ResultOfAppendingTwoStringsFirstStringInfoKey = @"firstString";
const NSString
*ResultOfAppendingTwoStringsSecondStringInfoKey = @"secondString";
const NSString
*ResultOfAppendingTwoStringsResultStringInfoKey = @"resultString";

//发送通知方法
- (void) broadcastNotification{
    
    NSString *firstName = @"Anthony";
    NSString *lastName = @"Robbins";
    NSString *fullName = [firstName stringByAppendingString:lastName];
    
    NSArray *objects = [[NSArray  alloc] initWithObjects:
                        firstName,
                        lastName,
                        fullName,
                        nil];
    NSArray *keys = [[NSArray alloc] initWithObjects: ResultOfAppendingTwoStringsFirstStringInfoKey, ResultOfAppendingTwoStringsSecondStringInfoKey, ResultOfAppendingTwoStringsResultStringInfoKey,
                     nil];
    NSDictionary *userInfo = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
    NSNotification *notificationObject =
    [NSNotification notificationWithName:(NSString *)ResultOfAppendingTwoStringsNotification object:self
                                userInfo:userInfo];
    [[NSNotificationCenter defaultCenter] postNotification:notificationObject];
    
}

//监听通知调用方法
- (void) appendingIsFinished:(NSNotification *)paramNotification
{
    
    NSLog(@"Notification is received.");
    NSLog(@"Notification Object = %@",[paramNotification object]);
    NSLog(@"Notification User-Info Dict = %@", [paramNotification userInfo]);
}


- (void)viewDidLoad
{
    //监听通知
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(appendingIsFinished:)
     name:(NSString *)ResultOfAppendingTwoStringsNotification object:self];
    
    
    //发送通知
    [self broadcastNotification];
    
}

posted @ 2014-06-26 10:51  天蓬大元帅  阅读(224)  评论(0编辑  收藏  举报