ios - UITextInputMode currentInputMode 已弃用。建议更换?

标签 ios iphone internationalization keyboard

在我们的应用中,我们想要检测当前的键盘语言。例如,如果用户在 Settings->General->Keyboard->Keyboards 下设置了多语言键盘,我们想知道他们输入的是什么语言,并在更改时从 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]。

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

最佳答案

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

UITextInputMode *currentInputMode = [notification object];

此外,您还可以获得特定响应器上的事件输入模式:

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

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

关于ios - UITextInputMode currentInputMode 已弃用。建议更换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22614489/

相关文章:

windows - qt lrelease windows二进制文件?

java - Spring 安全 2.0.5。自定义登录表单。看不到英语以外的语言错误

ios - 按名称获取核心数据行?

ios - 在动画其标题背景时单击 UIButton 显示错误行为

iphone - 如何在 iPhone 中解析 gdata xml?

jsf - JSF国际化,什么时候使用message-bundle和resource-bundle?

objective-c - NSMutableURLRequest 发送两次

ios - 是否可以在不将版本添加到App Store的情况下添加新的TestFlight版本?

iphone - iOS;如何(永久)缩放 UIimageView 然后 move 它

iphone - iOS 5 Storyboard自定义单元格崩溃 : UITableView dataSource must return a cell