ios开发系列之通知

1.通知的简述:通知实际上是在程序内部提供了消息广播的一种机制,不能在进程间进行通信.
2.通知的运行机制:通知中心(NSNotificationCenter)把接收到的消息(NSNotification),根据内部的一个消息转发表,来将消息转发给需要的对象,是基于观察者模式,它允许注册和删除观察者.
3.通知中两个重要的类:
NSNotificationCenter: 通知中心 ,由该类实现了观察者模式,并给开发者提供了注册,删除观察者的接口,每个程序内部有且仅有一个NSNotificationCenter对象,[NSNotificationCenter defaultCenter]指定一个默认的通知中心对象,通知的所有操作都是由该对象执行的,
NSNotification: 通知消息,是消息携带的载体,通过它可以把消息内容传递给观察者.


1.创建一个通知对象

    NSNotification * notif = [NSNotification notificationWithName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>];

注意,一般没有必要创建自己的通知.而是采用
[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>];来给该程序默认的通知中心发送通知了.


2.注册通知
[[NSNotificationCenter defaultCenter] addObserver:<#(id)#> selector:<#(SEL)#> name:<#(NSString *)#> object:<#(id)#>]; //添加一个观察者(addObserver) 在收到由对象(object)发出的对应名称(name)的通知后执行方法(selector)


3.发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:<#(NSString *)#> object:<#(id)#> userInfo:<#(NSDictionary *)#>]; //发送一个通知 指定通知的名称(name) 发送的对象(object) 和用户信息(userInfo)

发送消息不仅仅可以由用户发起,也可以由系统发起。


4.移除通知
监听通知以后一定要在监听通知的对象的dealloc方法中移除监听

    [[NSNotificationCenter defaultCenter] removeObserver:<#(id)#>];// 将观察者从通知中心移除

下面介绍ios中关于键盘通知,以及其用户信息:
键盘通知

UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification; //键盘即将出现
UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification;  //键盘已经出现
UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification; //键盘即将隐藏
UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification;  //键盘已经隐藏
UIKIT_EXTERN NSString *const UIKeyboardWillChangeFrameNotification //键盘将要改变其frame 
UIKIT_EXTERN NSString *const UIKeyboardDidChangeFrameNotification  //键盘已经改变其frame

键盘的userInfoKey

UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey  //开始时键盘的frame
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey    //结束时键盘的frame      
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey  //键盘动画期间的frame
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey   //键盘翻转时的信息 

我们可以根据键盘的通知和其userInfoKey来实现在textField上输入内容时让textField的高度随着键盘的frame变化,达到键盘不遮蔽输入框的目的

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 300, 220, 50)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.delegate =self;
    [self.view addSubview:textField];
    //以本身类对象作为观察者注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)keyboardWillChangeFrame:(id)sender{
    NSNotification * notif = (NSNotification *)sender;
    CGRect rect = [notif.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];//获取结束时键盘的frame
    CGFloat keyBoardY = rect.origin.y;
   [UIView animateWithDuration:0.25 animations:^{
       self.view.transform = CGAffineTransformMakeTranslation(0, keyBoardY - self.view.frame.size.height);
   }];
}
#pragma mark - UITextfieldDelegate方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [UIView animateWithDuration:0.25 animations:^{
        [textField resignFirstResponder];//放弃第一响应者,让键盘下去
    }];
    return YES;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];//移除监听通知的对象
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值