zoukankan      html  css  js  c++  java
  • 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];
        
    }

  • 相关阅读:
    Java导出Excel(附完整源码)
    Scala语言学习笔记——方法、函数及异常
    python时间序列按频率生成日期
    socket主要函数介绍
    JAVA版CORBA程序
    Linux文件压缩命令笔记
    使用JAVA写一个简单的日历
    Node.js 文件系统
    Node.js 常用工具util
    Node.js 全局对象
  • 原文地址:https://www.cnblogs.com/codeforcode/p/3809622.html
Copyright © 2011-2022 走看看