(ios)nsnotification总结

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

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

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject: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];

}

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

self.textbox.returnKeyType=UIReturnKeyDone;

self.textbox.delegate=self;

}- (BOOL)textFieldShouldReturn:(UITextView *)textView

{

[textView resignFirstResponder];returnYES;

}

2 自定义notification

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

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

}-(void)selfNotificationDO:(NSNotification *)aNotification

{//处理notification//........}

2.2 生成一个自定义notification

//生成一个自定义Notification[[NSNotificationCenter defaultCenter] postNotificationName:@"CustomNotification"object:self];

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航