UITextField 通知

cocoa的NSNotification类封装了广播的消息,有兴趣接收信息的对象将利用Cocoa的NSNotificationCenter类的实例注册它们自己。注册的对象称为观察者。

当匿名对象需要被动地观察和反应重要事件时,可以使用通知模式。与之相反,当匿名对象需要主动地影响所发生的事件时,可以使用委托模式。

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

 

UITextField *textField1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 200, 50)];
    textField1.tag = 10;
    textField1.borderStyle = UITextBorderStyleBezel;
    textField1.delegate = self;
    
    [self.view addSubview:textField1];
    //监听键盘出现的通知
    //获取通知中心
    NSNotificationCenter * nc =[NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyBoardWillhidden:) name:UIKeyboardWillHideNotification object:nil];
    
-(void)keyBoardWillhidden:(NSNotification *)notify
{
    NSLog(@"%@",@"键盘将被关闭");
}
-(void)textChange:(NSNotification *)notify
{
    UITextField *field = (id)notify.object;
    NSLog(@"%@",field.text);
}
-(void)keyBoardWillShow:(NSNotification *)notify
{
    NSDictionary *userinfo = notify.userInfo;
    
    NSLog(@"%@",@"---");
    CGFloat duration = [[userinfo valueForKey:@"UIKeyboardAnimationDurationUserInfoKey"] floatValue];
    [UIView animateWithDuration:duration animations:^{
        CGPoint center = self.view.center;
        self.view.center = CGPointMake(center.x, center.y - 250);
    }];
    
}

 

posted @ 2015-09-18 14:19  陌苏湮雪  阅读(311)  评论(0)    收藏  举报