不推荐使用UITextInputMode currentInputMode.建议更换?

Mat*_*ide 5 iphone keyboard internationalization ios

在我们的应用程序中,我们想要检测当前的键盘语言.例如,如果用户在"设置" - >"常规" - >"键盘" - >"键盘"下设置多个语言键盘,我们想知道他们输入的语言,并在此更改时从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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

小智 7

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

UITextInputMode *currentInputMode = [notification object];
Run Code Online (Sandbox Code Playgroud)

此外,您可以在特定响应者上获得活动输入模式:

UITextView *textView = [[UITextView alloc] init];
UITextInputMode *currentInputMode = textView.textInputMode;
Run Code Online (Sandbox Code Playgroud)

目前在iOS 7上,表情符号键盘的textInputMode/notification对象为nil.目前还不清楚这是否是预期的行为.


Suj*_*han -3

像这样更新您的代码:

NSArray *current = [UITextInputMode activeInputModes];
UITextInputMode *current = [current firstObject];
Run Code Online (Sandbox Code Playgroud)

  • 嗯,不起作用。活动输入模式的顺序并不反映哪一种是当前的模式。 (6认同)