UITextInputMode currentInputMode 已弃用。建议更换?

2024-05-13

在我们的应用程序中,我们想检测当前的键盘语言。例如,如果用户在“设置”->“常规”->“键盘”->“键盘”下设置了多种语言键盘,我们想知道他们正在输入什么语言,并在发生变化时从 NSNotificationCenter 收到通知。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSNotificationCenter *nCenter = [NSNotificationCenter defaultCenter];
    [nCenter addObserver:self selector:@selector(languageChanged:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
    [self languageChanged:nil];
}

-(void)languageChanged:(NSNotification*)notification
{
    for(UITextInputMode *mode in [UITextInputMode activeInputModes])
    {
        NSLog(@"Input mode: %@", mode);
        NSLog(@"Input language: %@", mode.primaryLanguage);
    }
    NSLog(@"Notification: %@", notification);
    UITextInputMode *current = [UITextInputMode currentInputMode];
    NSLog(@"Current: %@", current.primaryLanguage);
}

我们通过这段代码发现,当用户使用键盘上的地球图标切换键盘时,通知会正确触发,但是当我们迭代 UITextInputModes 时,它们以相同的顺序出现,没有(明显)指示哪个是当前的,除非我们使用现已弃用的 [UITextInputMode currentInputMode]。

我找不到任何文档表明苹果建议替代这个现已弃用的功能。有几个 SO 线程提到了弃用,但我没有找到解决方案。有任何想法吗?提前致谢。


通知 UITextInputCurrentInputModeDidChangeNotification 的对象是 UITextInputMode 的实例。

UITextInputMode *currentInputMode = [notification object];

此外,您还可以获取特定响应者的活动输入模式:

UITextView *textView = [[UITextView alloc] init];
UITextInputMode *currentInputMode = textView.textInputMode;

目前在 iOS 7 上,表情符号键盘的 textInputMode/notification 对象为零。目前尚不清楚这是否是有意的行为。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITextInputMode currentInputMode 已弃用。建议更换? 的相关文章

随机推荐