gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:39:01 25 4
gpt4 key购买 nike

在我们的应用中,我们想要检测当前的键盘语言。例如,如果用户在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com