我想要检测键盘的当前输入模式,并更改与其相关的文本方向(如果是阿拉伯语,则为rtl;如果是英语,则为ltr )。
在以下代码中:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(changeInputMode:)
name:UITextInputCurrentInputModeDidChangeNotification
object:nil
];
}
-(void)changeInputMode:(NSNotification *)notification
{
UITextInputMode *thisInputMode = [notification object];
NSLog(@"inputMethod=%@",thisInputMode);
}thisInputMode是nil!
如果我使用下面的代码:
NSString *inputMethod = [[UITextInputMode currentInputMode] primaryLanguage];
NSLog(@"inputMethod=%@",inputMethod);它工作得很好,可以检测当前的输入模式,但不推荐使用currentInputMode。
为什么[notification object]返回nil
发布于 2015-06-28 15:30:40
使用activeInputModes属性而不是currentInputMode
来自文档
数组中的每个元素都是UITextInputMode的一个实例。如果文本输入系统未设置此类实例,则返回空数组。
发布于 2016-02-20 09:07:24
它有一些新解决方案来获得当前的键盘输入模式,并且不使用过时的currentInputMode方法。
发布于 2018-09-21 21:05:02
textInputMode不是通知对象的一部分。通知会告诉您语言已更改。
要获取实际信息,您需要从changeInputMode:方法中调用第一个响应器(即textField)上的textInputMode.primaryLanguage。
https://stackoverflow.com/questions/31096696
复制相似问题