【问题标题】:UITextInputMode currentInputMode is deprecated. Suggested replacement?UITextInputMode currentInputMode 已弃用。建议更换?
【发布时间】:2014-05-02 02:38:22
【问题描述】:

在我们的应用中,我们想检测当前的键盘语言。例如,如果用户在 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 线程提到了弃用,但我没有找到解决方案。有任何想法吗?提前致谢。

【问题讨论】:

    标签: ios iphone internationalization keyboard


    【解决方案1】:

    像这样更新你的代码:

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

    【讨论】:

    • 嗯,没用。活动输入模式的顺序并不能反映当前显示的是哪一种。
    【解决方案2】:

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

    UITextInputMode *currentInputMode = [notification object];
    

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

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

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

    【讨论】:

      【解决方案3】:

      正确但愚蠢的答案是: 在属于视图层次结构的 UITextField 上使用 textInputMode。

      如果你碰巧(像我一样)没有一个(例如,因为你实现了一个 inputView 并且没有找到第一响应者的官方方法),可能看起来像这样:

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

      这非常明显和简单,只需要 3/4KB 的堆。与 UIKit-Library 中的几个字节的代码相比。这真的很有意义...... :-)

      【讨论】:

        【解决方案4】:
        [UIApplication sharedApplication].delegate.window.textInputMode
        

        它适用于 ios9。未在早期 iOS 版本上检查。

        【讨论】:

        • 顺便说一句, 不需要有窗口属性
        【解决方案5】:

        我认为您不应该创建新的UITextViewUITextField

        如果您的应用使用键盘,则您已经拥有一个。只需从现有文本控件中获取 .textInputMode.primaryLanguage

        如果您有多个文本视图/字段并且不知道哪一个处于活动状态,您可以在每个视图/字段上调用isFirstResponder。但看起来您可以从任何文本控件中获取textInputMode,而无需找到活动控件。

        【讨论】:

          【解决方案6】:

          Swift 2.0

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-05-21
            • 2013-09-18
            • 2016-04-13
            • 2014-02-12
            • 2021-12-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多