gpt4 book ai didi

objective-c - 键盘更改时是否会发送任何通知(例如从 NumberPad 到 Default)

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:19:56 26 4
gpt4 key购买 nike

我正在尝试了解如何在键盘更改时收到通知。我想做的是在类型 4 和 5(NumberPad 和 PhonePad)的键盘上添加一个 DONE 按钮,一切正常,除了当我使用默认 KB 类型从 TextField 转换时,通知 KeyboardDidAppear 不是被解雇。

这是我得到的:

- (void)viewDidLoad
{
[super viewDidLoad];

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

}

然后我为当前 KB 类型和当前正在编辑的 TextField 添加了一个属性:

#pragma mark - Delegate Methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.currentKBType = textField.keyboardType;
self.curTextField = textField;
return YES;
}

然后我根据当前知识库类型决定是否添加那个 DONE 按钮:

- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
[self addButtonToKeyboard];
}
}

问题是通知会在键盘显示时触发,但不会在键盘更改时触发(从一个 TextField 转换到另一个指定不同 KB 类型的 TextField。

有什么建议吗?我错过了什么吗?

最佳答案

明白了。有点逻辑,但它完美无缺。这是我所做的:添加私有(private)属性:

@property (nonatomic) UIKeyboardType currentKBType;
@property(nonatomic,strong) UITextField *curTextField;
@property(nonatomic,strong) UIButton *doneButton;
@property(nonatomic) BOOL doneButtonDisplayed;

然后在两个 TextField 委托(delegate)方法中添加以下逻辑:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.currentKBType = textField.keyboardType;
if (textField.keyboardType == 4 || textField.keyboardType == 5) {
if (!doneButtonDisplayed) {
[self addButtonToKeyboard];
}
} else {
if (doneButtonDisplayed) {
[self removeButtonFromKeyboard];
}
}

self.curTextField = textField;
return YES;
}

在我在 viewDidLoad 中注册 VC 的 KeyboardDidShowNotification 中:

- (void)keyboardDidShow:(NSNotification *)note {
if (self.currentKBType == 4 || self.currentKBType == 5) {
if (!doneButtonDisplayed) {
[self addButtonToKeyboard];
}
} else {
if (doneButtonDisplayed) {
[self removeButtonFromKeyboard];
}
}
}

以及这些方法中引用的两个方法:

- (void)addButtonToKeyboard {

// create custom button
doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;

[doneButton setImage:[UIImage imageNamed:@"DoneNormal.png"] forState:UIControlStateNormal];
[doneButton setImage:[UIImage imageNamed:@"DoneHL.png"] forState:UIControlStateHighlighted];

[doneButton addTarget:self action:@selector(resignKeyboard) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
if ([[[UIApplication sharedApplication] windows] count] > 1) {
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i=0; i<[tempWindow.subviews count]; i++) {
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) {
[keyboard addSubview:doneButton];
self.doneButtonDisplayed = YES;
}
}

}
}

- (void)removeButtonFromKeyboard {
[doneButton removeFromSuperview];
self.doneButtonDisplayed = NO;
}

最后,每当触摸“完成”按钮时调用 resignKeyboard 方法:

-(void)resignKeyboard {
[self.curTextField resignFirstResponder];
self.doneButtonDisplayed = NO;
}

这将在显示 NumberPad 或 PhonePad 类型的键盘时添加完成按钮,并将它从其他键盘类型中删除(仅当已添加时)。

经过测试,效果很好。

关于objective-c - 键盘更改时是否会发送任何通知(例如从 NumberPad 到 Default),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897425/

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