1 文本輸入,鍵盤顯示時,view向上,鍵盤隱藏時,view向下
1.1 注冊鍵盤顯示,關閉通知,并實現主界面上下變動
[[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]; }
1.2 文本框初始化,并實現UITextViewDelegate委托
self.textbox.returnKeyType=UIReturnKeyDone; self.textbox.delegate=self;} - (BOOL)textFieldShouldReturn:(UITextView *)textView{ [textView resignFirstResponder]; return YES;}
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];
新聞熱點
疑難解答