- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试了解如何在键盘更改时收到通知。我想做的是在类型 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/
我有一些简单的 UITextFields 可以调出数字键盘。 我想限制用户对输入的控制。 例如,一个特定的文本字段不应允许超过 32 的任何值。 我应该将哪个参数与 32 ) ret
默认情况下,在 iPhone 上使用 .numberPad 键盘时,数字键在每个键的底部也有字母。 这些字母纯粹是装饰性的,以帮助人们输入数字;但就我而言(输入商品数量),它们完全是多余的,甚至可能令
模拟器曾经在数字锁定打开的情况下接受来 self 的数字键盘的数字输入,但是现在,它的行为就像数字锁定关闭一样。例如,如果我在数字键盘上键入“7”,它的响应就像按下了“Home”一样。 顶部的数字键仍
我在我的 android 应用程序中实现了一个 WebView 来显示一个网络表单。其中一个字段要求用户手动输入他的电话号码(以防电话不允许提取电话号码)。 现在,当用户选择字段时,qwerty 软键
我在数字键盘上有一个完成按钮,当我输入一个值并按下完成时,该值没有保存/注册。我的代码如下: 我正在使用 UIRepresentable 协议(protocol)(由 Rajeev Kumar S 提
UIKeyboardType.NumberPad 看起来像这样: 我希望它看起来像这样: 有没有办法在键盘启动时在那个角落覆盖一个 View 来添加这个单一的键?这在 Swift 4 中可行吗? 我这
我在 Storyboard 上为我的 TextField 设置了 Number Pad,因为我的应用只需要数字。但它仅适用于 iPhone。我找不到适用于 iPad 的解决方案。我试过了 overri
UIKeyboardTypeNumberPad 在 iPad 上不显示数字键盘。相反,它显示 UIKeyboardTypeNumbersAndPunctuation 键盘。 我是否遗漏了某些内容,或者
对 iOS 开发相当陌生,所以请原谅我问一些可能非常明显的问题。众所周知,UITextField 的键盘(keyboardType 设置为 .NumberPad)如下所示... .NumberPad
我搜索如何使用 Swift 5 在文本字段的数字键盘中用点替换逗号分隔符。 我尝试过,但没有成功。 let commaValue = textField.text! let decimalValue
我正在使用 TextField 从特定形式的用户号码中获取 xxx-xxx-xx-xx。 我需要向用户显示 NumberPad 键盘并在编辑期间在第 3、6 和第 8 位数字后添加分隔符“-”。 问题
对 iOS 开发还很陌生,所以请原谅我问了一些可能很明显的问题。众所周知,将 keyboardType 设置为 .NumberPad 的 UITextField 键盘看起来如下所示... .Numbe
我在代码中创建了一个 UITextField,现在我知道如何定义应该出现的键盘了吗?我刚拿到标准键盘,但我需要数字键盘?! 谢谢 最佳答案 您需要将keyboardType 属性设置为UIKeyboa
这个问题在这里已经有了答案: How to show "Done" button on iOS number pad keyboard? (21 个回答) 已关闭 9 年前。
使用 UIButtons 创建一个简单的按钮矩阵非常简单。我坚持的是它的行为。多个 UIButton 的行为不同。当触摸离开时,UIKeyboard 上的按键会失去其突出显示状态,自定义按键会在相当长
我正在尝试了解如何在键盘更改时收到通知。我想做的是在类型 4 和 5(NumberPad 和 PhonePad)的键盘上添加一个 DONE 按钮,一切正常,除了当我使用默认 KB 类型从 TextFi
我目前正在将我的应用程序更新到 iOS 11。突然我的键盘出现此故障/错误: Can't find keyplane that supports type 4 for keyboard iPhone-
我最近升级到 Xcode 7,今天当我点击一个文本字段并弹出键盘时,我在日志中注意到这个警告: Can't find keyplane that supports type 4 for keyboar
我有一个带有自定义设计的 xamarin.ios 应用程序,我想根据我的设计调整键盘。所以我正在尝试为 UIKeyboardType.NumberPad 连接一个自定义设计,类似于下图。我该怎么做?谢
我已经下载了适用于 iPhone 的 iOS 8 Gold Master 和 SDK。 我测试了该应用程序,除了一件事之外,它运行良好。 我有一个文本字段,如果用户想要输入内容,其中会出现数字键盘,此
我是一名优秀的程序员,十分优秀!