码迷,mamicode.com
首页 > 其他好文 > 详细

UITextField 通知

时间:2015-09-18 15:08:37      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }];
    
}

 

UITextField 通知

标签:

原文地址:http://www.cnblogs.com/mosuyanxue/p/4819101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!