简体   繁体   English

iPhone键盘,自定义处理

[英]iPhone keyboard, custom handling

I need to react in a way specific to my iPhone app when the user is typing inside some UITextView field. 当用户在某些UITextView字段中键入内容时,我需要以特定于我的iPhone应用程序的方式做出反应。

Three examples : 三个例子:

  1. I want to dismiss the keyboard if the user types in: 'E'. 如果用户输入“ E”,我想关闭键盘。
  2. I want to jump to a new input line if the user types in: 'nN'. 如果用户键入:'nN',我想跳到新的输入行。
  3. I want to ignore if the user types in: 'A' or 'b'. 我想忽略用户是否输入:“ A”或“ b”。

I am sure there must be a quite simple way to do what I want. 我敢肯定,必须有一种非常简单的方法来做我想要的事情。 But after browsing for a while on the net, I do not find a clear answer. 但是在网上浏览了一段时间后,我没有找到明确的答案。

What is the way to go? 怎么走?

Thanks for any tip. 感谢您的提示。

If you are using a UITextField , give the text field a delegate (implementing the UITextFieldDelegate protocol), and in the delegate, implement textField:shouldChangeCharactersInRange:replacementString: . 如果您使用的是UITextField ,请为文本字段提供一个委托(实现UITextFieldDelegate协议),并在委托中实现textField:shouldChangeCharactersInRange:replacementString: Examine the replacement string and take action based on whether it contains the characters you're looking for. 检查替换字符串,并根据替换字符串是否包含要查找的字符采取措施。

If you are using a UITextView , give the text view a delegate (implementing the UITextViewDelegate protocol), and in the delegate, implement textView:shouldChangeTextInRange:replacementText: . 如果您使用的是UITextView ,请为文本视图提供一个委托(实现UITextViewDelegate协议),并在委托中实现textView:shouldChangeTextInRange:replacementText:

You could try implementing this in your UITextViewDelegate: 您可以尝试在UITextViewDelegate中实现它:

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html

Check the replacementText . 检查replacementText If it's @"e", call resignFirstResponder on the UITextView and return NO. 如果它是@“ e”, resignFirstResponder在UITextView上调用resignFirstResponder并返回NO。

If it's @"A" or @"b", return NO. 如果是@“ A”或@“ b”,则返回NO。

I'm not exactly sure about the other one, but you can probably handle it here also and return NO. 我不确定另一个,但是您也可以在这里处理它并返回NO。

I simple way to just flat out deny A and B is to do a simple thing like this (You would get up a check timer to run the void) 我可以简单地拒绝A和B的简单方法是执行类似这样的简单操作(您将启动一个检查计时器来运行void)

- (void) something {

if([statusString isEqualToString:@"a"]){
    statusString == @"";
}

}

This would simply delete every thing if the user types a. 如果用户键入a,这将简单删除所有内容。 This may not be exactly what you want but I wanted to give a start 这可能不完全是您想要的,但我想开始

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM