}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
Class at a Glance
The NSNotificationCenter class provides a way to send notifications to objects in the same task. It takes NSNotification objects and broadcasts them to any objects in the same task that have registered to receive the notification with the task’s default notification center.
即全局变量一般,在同一个程序中,只要定义了一个通知,如果没有remove遇到相同对象,同样的消息(入上面代码中的UIKeyboardWillShowNotification)同样的方法都回被调用(入上面代码中的:keyboardWillShow:)。
所以使用完毕后最好 removeObserver
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
- (void)keyboardWillShow:(NSNotification*)aNotification {
if (!maskViewShow){
//if(maskView != nil){
[maskView removeFromSuperview];
//[maskView release];
//maskView = nil;
//}
return;;
}
NSDictionary* info = [aNotification userInfo];
/* 获取键盘区域的CGRect值 */
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect maskRect ;//=[CGRect alloc];
[aValue getValue:&maskRect]; //宽高
//[bValue getValue:&maskRect]; //中心点坐标
//maskRect.origin
//maskView =[[UIView alloc] initWithFrame: maskRect];
maskView.frame = maskRect;
maskView.alpha =0.7;
maskView.backgroundColor = [UIColor blackColor] ;
//[.view addSubview:maskView];
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
/*便利window 上的所以view 找到 UIKeyboard 添加maskView*/
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
//NSLog(@"%@",[keyboard description]);
// keyboard view found; add the custom button to it
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
[keyboard addSubview:maskView];
break;
}
}
}
这样自定义键盘在点Done时,如果只执行 [curTextField resignFirstResponder]; 不会触发控制类的textFieldShouldReturn 委托方法,解决办法:
在 [curTextField resignFirstResponder]之前,调用textFieldShouldReturn
if ([curViewController respondsToSelector:@selector(textFieldShouldReturn:)]){
[curViewController performSelector:@selector(textFieldShouldReturn:) withObject:curTextField];
}
改变iPhone键盘颜色的代码