gpt4 book ai didi

iphone - KeyBoardWIllShowNotification 调用一次的原因是什么?

转载 作者:行者123 更新时间:2023-12-03 20:22:38 25 4
gpt4 key购买 nike

我正在使用 keyboardWasShownkeyboardWillBeHidden 通知来滑动 View 以获取可见的 TextView 。

我有一个带有六个选项卡的 UITabBar 应用程序。

在每个 View 中我都使用 UINavigationController

在每个 UITableViewCell 的详细 View 中,我使用键盘通知。

所以问题是键盘通知在我第一次使用时起作用。在其他选项卡上打开它将不起作用。

代码如下:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification
object:nil];

和方法

- (void)keyboardWasShown:(NSNotification *)aNotification {
if ( keyboardShown )
return;


NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize.height-100;
frame.size.height += keyboardSize.height-100;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];

viewMoved = YES;

keyboardShown = YES;
}
- (void)keyboardWasHidden:(NSNotification *)aNotification {
if ( viewMoved && tvAddreview) {
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-100;
frame.size.height -= keyboardSize.height-100;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];

viewMoved = NO;
}

keyboardShown = NO;
}

最佳答案

你应该在每个类中这样做:

-(void) viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[nc addObserver:self
selector:@selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification
object:nil];

}

- (void) viewWillDisappear: (BOOL)animated{

[super viewWillDisappear:animated];

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[nc removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

因为通知是在应用程序级别而不是您的类(class)级别。因此,如果您已将它们添加到一个类中而不是所有类中,则转到下一类。通知仍会调用键 keyboardWasShown: 以及您添加通知的类中的另一个键,因此您的本地变量如... View 移动=是;

keyboardShown = YES;

会抛出坏的多余异常

在您的情况下,还需要在所有 6 个 View Controller 中执行此操作

希望这有帮助。

关于iphone - KeyBoardWIllShowNotification 调用一次的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com