여기에 문제가 있습니다. 프로그램을 디버깅 할 때마다 keyboardWillShow 함수가 매번 응답하지 않습니다. 단지 처음에는 프로그램에 의해 호출됩니다. 여기 내 코드입니다, 내 코드에서 뭐가 잘못 됐는지 모르지만, 키보드가 처음 나타 났을 때 함수는 잘 돌아갑니다.keyboardWillShow가 응답하지 않습니다.
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.textview.frame;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
textview.frame = newTextViewFrame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
// textview.frame = self.view.bounds;
[self save];
[UIView commitAnimations];
}
와 나는 REGIST 통지
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
내가 firstresponder을 사임
- (void)viewDidUnload
{
[super viewDidUnload];
[self save];
self.textview = nil;
self.title = nil;
self.tags = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
여기에서 그것을 제거, 여기
- (IBAction)save:(id)sender {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNote)] autorelease];
[textview resignFirstResponder];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(save:)] autorelease];
[self setUpUndoManager];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
[self save];
return YES;
}
당신은 더 많은 정보가 필요합니까, 첫 번째 응답을 릴리스합니까? 무슨 일이 일어나고 두 번째 등 –
예, 나는 FirstResponder를 사임, 여기 내 코드입니다 - (BOOL) textFieldShouldBeginEditing : (UITextField *) textField { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem : UIBarButtonSystemItemDone target : 자기 행동 : @selector (save :)] autorelease]; [self setUpUndoManager]; [textField resignFirstResponder]; return 예; - (BOOL) textFieldShouldEndEditing : (UITextField *) textField { [자동 저장]; [textField resignFirstResponder]; return YES; } – leoyfm
키보드가 두 번째로 표시 될 때 keyboardWillShow 메서드가 자동으로 호출되지 않습니다. 즉 keyboardWillShow 메서드는 키보드 팝업이 처음 실행될 때만 실행됩니다. 그 후에이 방법은 다시는 실행되지 않습니다. – leoyfm