我正在为iOS开发一个自定义键盘,使用iOS实现了以下方法:
我想知道用户何时更改当前文本字段并移动到另一个文本字段,以便配置键盘中当前按下的文本字段的自动大写参数。
现在我试图做这样的事情:
override func textWillChange(textInput: UITextInput) {
if !textInput.isEqual(self.mCurrentTextInput) {
Logger.printLogToConsole(TAG, aMethodName: __FUNCTION__, aMessage: "TextInput has changed!")
KeyboardState.sharedInstance.setAutoCapitalizationFlag()
}
self.mCurrentTextInput = textInput
}甚至像这样的东西:
override func textWillChange(textInput: UITextInput) {
if self.mCurrentTextInput == nil {
if let view = textInput.textInputView {
self.mCurrentTextInput = view
KeyboardState.sharedInstance.setAutoCapitalizationFlag()
}
Logger.printLogToConsole(TAG, aMethodName: __FUNCTION__, aMessage: "TextInput has changed!")
} else if self.mCurrentTextInput!.isEqual(textInput) {
Logger.printLogToConsole(TAG, aMethodName: __FUNCTION__, aMessage: "TextInput has changed!")
if let view = textInput.textInputView {
KeyboardState.sharedInstance.setAutoCapitalizationFlag()
self.mCurrentTextInput = view
}
}
}但这些选择对我都没有用。
更新:您可以注意到,我在回调方法中接收的对象不是UITextField对象,而是实际上是协议的UITextInput对象,而不是文本字段本身。所以我不能把观察者加进去。我的键盘应用程序不控制用户输入文本的字段。
问题:如何识别用户更改当前文本字段的时刻?
发布于 2015-06-03 10:02:45
我使用的是目标C,因为我现在有这样的代码:-
您可以使用KVO,我想您的目标是:-
static void *contextNew = &contextNew;
//Observe change to txtfeild.text:
[self.txtfeild addObserver:self forKeyPath:@"text"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
context:contextNew];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if(context == contextNew) {
NSLog(@"txtfeild new text : %@ - txtfeild Old text: %@",
[change objectForKey:NSKeyValueChangeNewKey],
[change objectForKey:NSKeyValueChangeOldKey]);
}
}https://stackoverflow.com/questions/30616626
复制相似问题