首页 > 其他 > 详细

(ios) nsnotification总结

时间:2014-01-20 19:31:25      阅读:442      评论:0      收藏:0      [点我收藏+]

1  文本输入,键盘显示时,view向上,键盘隐藏时,view向下

1.1 注册键盘显示,关闭通知,并实现主界面上下变动

bubuko.com,布布扣
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


-(void)keyboardWillShow:(NSNotification *)aNotification
{

    CGRect keyBoardRect=[[[aNotification userInfo]objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
    
    NSTimeInterval animalInterval=[[[aNotification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame=self.view.frame;
    frame.origin.y=-keyBoardRect.size.height;
    [UIView beginAnimations:@"keyboardshow" context:nil];
    [UIView setAnimationDuration:animalInterval];
    self.view.frame=frame;
    [UIView commitAnimations];


}

-(void)keyboardWillHide:(NSNotification *)aNotification
{

    NSTimeInterval animalInterval=[[[aNotification userInfo]objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    CGRect frame=self.view.frame;
    frame.origin.y=0;
    [UIView beginAnimations:@"keyboardhide" context:nil];
    [UIView setAnimationDuration:animalInterval];
    self.view.frame=frame;
    [UIView commitAnimations];
    
}
bubuko.com,布布扣

1.2 文本框初始化,并实现UITextViewDelegate委托

bubuko.com,布布扣
    self.textbox.returnKeyType=UIReturnKeyDone;
    self.textbox.delegate=self;
}

 - (BOOL)textFieldShouldReturn:(UITextView *)textView
{
 [textView resignFirstResponder];
    return YES;
}
bubuko.com,布布扣

 

2 自定义notification

 2.1 定义侦听自定义notification观察者

bubuko.com,布布扣
//注册观察者,侦听自定义通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selfNotificationDO:) name:@"CustomNotification" object:nil];

}

-(void)selfNotificationDO:(NSNotification *)aNotification
{
     //处理notification
    //........
}
bubuko.com,布布扣

2.2 生成一个自定义notification

bubuko.com,布布扣
//生成一个自定义Notification
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CustomNotification" object:self];
bubuko.com,布布扣

(ios) nsnotification总结

原文:http://www.cnblogs.com/macroxu-1982/p/3526632.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!