不推荐使用UITextInputMode currentInputMode.建议更换? [英] UITextInputMode currentInputMode is deprecated. Suggested replacement?

查看:393
本文介绍了不推荐使用UITextInputMode currentInputMode.建议更换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用中,我们想检测当前的键盘语言.例如,如果用户在设置"->常规"->键盘"->键盘"下设置了多种语言的键盘,我们希望知道他们所输入的语言,并在更改时从NSNotificationCenter收到通知.

In our app, we'd like to detect the current keyboard language. For example, if the user sets up multiple language keyboards under Settings->General->Keyboard->Keyboards, we'd like to know what language they're typing in and to get a notification from NSNotificationCenter when this changes.

- (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);
}

我们在这段代码中发现的是,当用户使用键盘上的Globe图标切换键盘时,通知会正确触发,但是当我们迭代UITextInputModes时,它们以相同的顺序出现,没有(明显)指示除非我们使用现在不建议使用的[UITextInputMode currentInputMode],否则它是当前的.

What we've discovered with this code is that the notification fires appropriately when the user toggles keyboards using the globe icon on the keyboard, but when we iterate the UITextInputModes, they appear in the same order with no (apparent) indication of which is the current one unless we use the now-deprecated [UITextInputMode currentInputMode].

我找不到任何文档来表明Apple建议使用此功能,而不是现在不推荐使用的功能.有几个SO线程提到了弃用,但是我在解决方案中都找不到.有任何想法吗?预先感谢.

I can't find any documentation indicating Apple's suggested alternative to this now-deprecated functionality. There are several SO threads mentioning the deprecation, but none that I found with solutions. Any ideas? Thanks in advance.

推荐答案

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

The object for the notification UITextInputCurrentInputModeDidChangeNotification is an instance of UITextInputMode.

UITextInputMode *currentInputMode = [notification object];

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

Also, you can get the active input mode on a particular responder:

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

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

Currently on iOS 7, the textInputMode/notification object are nil for the emoji keyboard. It's unclear if this is intended behaviour.

这篇关于不推荐使用UITextInputMode currentInputMode.建议更换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆