UITextInputMode currentInputMode is deprecated. Suggested replacement?

你离开我真会死。 提交于 2019-12-07 07:03:13

问题


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

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].

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.


回答1:


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;

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




回答2:


I don't think you should create new UITextView or UITextField.

You already have one if your app uses keyboard. Just get .textInputMode.primaryLanguage from your existing text control.

If you have several text views/field and don't know, which one is active, you can call isFirstResponder on each. But looks like you can get textInputMode from any text control and don't need to find active one.




回答3:


The correct but stupid answer is: use textInputMode on a UITextField that is part of the view-hirarchy.

If you happen (like me) to not have one (eg because you implement an inputView and have no official way of finding the first responder) that might look like this:

UTextField* p = [[UITextField alloc] init];
p.hidden = YES;
[someView addSubview: p];
... = p.textInputMode.primaryLanguage;
[p removeFromSuperview];

That's so obvious and simple and only needs 3/4KB of heap. Compared to a few bytes of the code in the UIKit-Library. That really makes sense... :-)




回答4:


[UIApplication sharedApplication].delegate.window.textInputMode

It works on ios9. Not checked on earlier iOS versions.




回答5:


Swift 2.0

//richT is the TextView or textField
var language = richT.textInputMode?.primaryLanguage
print("language: \(language)")
//UITextInputMode().primaryLanguage - returns nil value



回答6:


Update your code like this:

NSArray *current = [UITextInputMode activeInputModes];
UITextInputMode *current = [current firstObject];


来源:https://stackoverflow.com/questions/22614489/uitextinputmode-currentinputmode-is-deprecated-suggested-replacement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!