zoukankan      html  css  js  c++  java
  • [iOS基础控件

    A.定义
         iOS程序都有一个NSNotificationCenter的单例对象,用来负责发布不同对象之间的通知
         任何对象都能够在NSNotificationCenter发布通知,发布自己的发生的事件;监听者(Observer)可以选择接受某个特定通知。
    Image(107)
     
     
    B.使用
     
    1. 属性
    •      - (NSString*)name; // 通知的名称
    •      - (id)object; // 通知发布者(是谁要发布通知)
    •      - (NSDictionary*)userInfo; // 一些额外的信息(通知发布者传递给通知接收者的信息内容)
     
              2.初始化
    •      + (instancetype)notificationWithName:(NSString*)aName object:(id)anObject;

    •      + (instancetype)notificationWithName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;

    •      - (instancetype)initWithName:(NSString*)name object:(id)object userInfo:(NSDictionary *)userInfo;
     
     
    3.发布通知(通过NSNotificationCenter)
         - (void)postNotification:(NSNotification*)notification;
         发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等
     
         - (void)postNotificationName:(NSString*)aName object:(id)anObject;
         发布一个名称为aName的通知,anObject为这个通知的发布者
     
         - (void)postNotificationName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;
         发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
     
     
    4.设置监听器Observer
    通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)
    (1)
         - (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString*)aName object:(id)anObject;
     
         observer:监听器,即谁要接收这个通知
         aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入
         aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知
         anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
     
    (2)
         - (id)addObserverForName:(NSString*)name object:(id)obj queue:(NSOperationQueue*)queue usingBlock:(void (^)(NSNotification*note))block;
         name:通知的名称
         obj:通知发布者
         block:收到对应的通知时,会回调这个block
         queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行
     
    5.在监听器销毁之前,取消监听
                   通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃
     
                   通知中心提供了相应的方法来取消注册监听器
           - (void)removeObserver:(id)observer;
           - (void)removeObserver:(id)observer name:(NSString*)aName object:(id)anObject;
     
                   一般在监听器销毁之前取消注册(如在监听器中加入下列代码):

    - (void)dealloc {

      //[super dealloc];  非ARC中需要调用此句

        [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

     
    C.iOS中自带的通知 UIDevice
              UIDevice类提供了一个单例对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)
     
              通过[UIDevice currentDevice]可以获取这个单例对象
     
              UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:
         UIDeviceOrientationDidChangeNotification// 设备旋转
         UIDeviceBatteryStateDidChangeNotification// 电池状态改变
         UIDeviceBatteryLevelDidChangeNotification // 电池电量改变
         UIDeviceProximityStateDidChangeNotification// 近距离传感器(比如设备贴近了使用者的脸部)
     
    D.键盘通知
              我们经常需要在键盘弹出或者隐藏的时候做一些特定的操作,因此需要监听键盘的状态
     
              键盘状态改变的时候,系统会发出一些特定的通知
         UIKeyboardWillShowNotification // 键盘即将显示
         UIKeyboardDidShowNotification //键盘显示完毕
         UIKeyboardWillHideNotification // 键盘即将隐藏
         UIKeyboardDidHideNotification // 键盘隐藏完毕
         UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
         UIKeyboardDidChangeFrameNotification //键盘的位置尺寸改变完毕
     
              系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的key如下:
         UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的frame
         UIKeyboardFrameEndUserInfoKey // 键盘最终的frame(动画执行完毕后)
         UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
         UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
     
    E.通知和代理的异同
              共同点
              利用通知和代理都能完成对象之间的通信
         (比如A对象告诉D对象发生了什么事情, A对象传递数据给D对象)
              不同点
              代理 :一对一关系(1个对象只能告诉另1个对象发生了什么事情)
              通知 :多对多关系(1个对象能告诉N个对象发生了什么事情, 1个对象能得知N个对象发生了什么事情)
     
     
    F.练习
         模拟敌我电台向外发布消息,我方人员接收我方电台,敌方人员接收敌方电台,内奸可以接收双方电台
     
     1 //
     2 //  Radio.h
     3 //  Notification
     4 //
     5 //  Created by hellovoidworld on 14/12/7.
     6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface Radio : NSObject
    12 
    13 // 电台名称
    14 @property(nonatomic, copy) NSString *name;
    15 
    16 @end
     
     1 //
     2 //  Radio.m
     3 //  Notification
     4 //
     5 //  Created by hellovoidworld on 14/12/7.
     6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "Radio.h"
    10 
    11 @implementation Radio
    12 
    13 @end
     
     1 //
     2 //  main.m
     3 //  Notification
     4 //
     5 //  Created by hellovoidworld on 14/12/7.
     6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "Radio.h"
    11 #import "Person.h"
    12 
    13 int main(int argc, const char * argv[]) {
    14     @autoreleasepool {
    15         Radio *r1 = [[Radio alloc] init];
    16         r1.name = @"我方电台";
    17        
    18         Radio *r2 = [[Radio alloc] init];
    19         r2.name = @"敌方电台";
    20        
    21         // 仅能接收我方电台消息
    22         Person *p1 = [[Person alloc] init];
    23         p1.role = @"我方忠心耿耿的战士";
    24        
    25         // 仅能接收敌方电台消息
    26         Person *p2 = [[Person alloc] init];
    27         p2.role = @"敌方可恶的走狗";
    28        
    29         // 技能接收我方电台消息,也能接收敌方电台消息
    30         Person *p3 = [[Person alloc] init];
    31         p3.role = @"潜伏在我方狡猾的内奸";
    32        
    33         // 嗯,我是传播电台电波的媒介
    34         NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    35        
    36         // 1.添加监听器
    37         [center addObserver:p1 selector:@selector(receiveInfoFromRadio:) name:@"attack" object:r1];
    38         [center addObserver:p2 selector:@selector(receiveInfoFromRadio:) name:@"defend" object:r2];
    39         [center addObserver:p3 selector:@selector(receiveInfoFromRadio:) name:@"attack" object:r1];
    40         [center addObserver:p3 selector:@selector(receiveInfoFromRadio:) name:@"defend" object:r2];
    41        
    42         // 2.我方电台r1发布攻击信息
    43         [center postNotificationName:@"attack" object:r1 userInfo:@{@"title":@"attack!, all the money belong to us!", @"title2":@"And the girls!!!"}];
    44        
    45         // 3.敌方电台r2发布回防信息
    46         [center postNotificationName:@"defend" object:r2 userInfo:@{@"title":@"TP back quickly!"}];
    47        
    48     }
    49     return 0;
    50 }
     
    out:
    2014-12-07 21:18:03.142 Notification[7923:714964] 我是我方忠心耿耿的战士, 正在接受我方电台的消息, 内容是{
        title = "attack!, all the money belong to us!";
        title2 = "And the girls!!!";
    }
    2014-12-07 21:18:03.143 Notification[7923:714964]
     我是潜伏在我方狡猾的内奸, 正在接受我方电台的消息, 内容是{
        title = "attack!, all the money belong to us!";
        title2 = "And the girls!!!";
    }
    2014-12-07 21:18:03.143 Notification[7923:714964]
     我是敌方可恶的走狗, 正在接受敌方电台的消息, 内容是{
        title = "TP back quickly!";
    }
    2014-12-07 21:18:03.143 Notification[7923:714964]
     我是潜伏在我方狡猾的内奸, 正在接受敌方电台的消息, 内容是{
        title = "TP back quickly!";
    }
    Program ended with exit code: 0
     
     
  • 相关阅读:
    java编译错误No enclosing instance of type TestFrame is accessible. Must qualify the allocation with an enclosing instance of type TestFrame (e.g. x.new A(
    java 2中创建线程方法
    动态规划基本思想
    关于eclipse编译一个工程多个main函数
    java Gui初识
    Eclipse中java项目的打包
    java 播放声音
    把资源文件夹导入到eclipse中
    Java建立JProgressBar
    How to grant permissions to a custom assembly that is referenced in a report in Reporting Services
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4149897.html
Copyright © 2011-2022 走看看