我正在开发一个自定义iOS键盘 - 其中大部分已经到位,但我在删除键时出现问题,更具体地说是删除整个单词。
问题是即使文本字段中还有字符,self.textDocumentProxy.documentContextBeforeInput?.isEmpty也会返回Nil。
以下是背景资料: 如果您不熟悉,它在iOS键盘上的工作方式是,在按住退格键时,系统一次删除一个字符(对于前10个字符)。在10个字符后,它开始删除整个单词。
在我的代码的情况下,我删除10个单个字符然后我成功删除了几个完整的单词,然后突然self.textDocumentProxy.documentContextBeforeInput?.isEmpty返回Nil,即使文本中还有剩余字符字段。
我看了很多文档和网页,我没有看到其他人有同样的问题,所以我确定我错过了一些明显的东西,但我很困惑。
以下是我班级定义的相关部分:
class KeyboardViewController: UIInputViewController {
//I've removed a bunch of variables that aren't relevant to this question.
var myInputView : UIInputView {
return inputView!
}
private var proxy: UITextDocumentProxy {
return textDocumentProxy
}
override func canBecomeFirstResponder() -> Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
//proxy let proxy = self.textDocumentProxy
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillAppear"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name: UIKeyboardWillHideNotification, object: nil)
self.myInputView.translatesAutoresizingMaskIntoConstraints = true
// Perform custom UI setup here
view.backgroundColor = UIColor(red: 209 / 255, green: 213 / 255, blue: 219 / 255, alpha: 1)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "touchUpInsideLetter:", name: "KeyboardKeyPressedNotification", object: nil)
showQWERTYKeyboard()
}
我为退格按钮设置了监听器和操作,因为我还在按钮上配置了一堆其他属性。我在Switch中这样做 - 相关部分在这里:
case "<<":
isUIButton = true
normalButton.setTitle(buttonString, forState: UIControlState.Normal)
normalButton.addTarget(self, action: "turnBackspaceOff", forControlEvents: UIControlEvents.TouchUpInside)
normalButton.addTarget(self, action: "turnBackspaceOff", forControlEvents: UIControlEvents.TouchUpOutside)
normalButton.addTarget(self, action: "touchDownBackspace", forControlEvents: UIControlEvents.TouchDown)
let handleBackspaceRecognizer : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleBackspaceLongPress:")
normalButton.addGestureRecognizer(handleBackspaceRecognizer)
buttonWidth = standardButtonWidth * 1.33
nextX = nextX + 7
感谢您的任何想法。
****修改原帖以帮助更多地了解问题**** 以下是用于创建退格行为的4个函数。它们似乎至少在前两个单词中正常工作,但是正确建议的可选检查开始评估为nil并且它会停止删除。
//Gets called on "Delete Button TouchUpInside" and "Delete Button TouchUpOutside"
func turnBackspaceOff() {
self.backspaceIsPressed = false
keyRepeatTimer.invalidate()
}
//Handles a single tap backspace
func touchDownBackspace() {
(textDocumentProxy as UIKeyInput).deleteBackward()
}
//Handles a long press backspace
func handleBackspaceLongPress(selector : UILongPressGestureRecognizer) {
if selector.state == UIGestureRecognizerState.Began {
self.backspaceIsPressed = true
self.keyRepeatTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "backspaceRepeatHandlerFinal", userInfo: nil, repeats: true)
print("handleBackspaceLongPress.Began")
}
else if selector.state == UIGestureRecognizerState.Ended {
self.backspaceIsPressed = false
keyRepeatTimer.invalidate()
numberOfKeyPresses = 0
print("handleBackspaceLongPress.Ended")
}
else {
self.backspaceIsPressed = false
keyRepeatTimer.invalidate()
numberOfKeyPresses = 0
print("handleBackspaceLongPress. Else")
}
}
func backspaceRepeatHandlerFinal() {
if let documentContext = proxy.documentContextBeforeInput as String? {
print(documentContext)
}
print("backspaceRepeatHandlerFinal is called")
if self.backspaceIsPressed {
print("backspace is pressed")
self.numberOfKeyPresses = self.numberOfKeyPresses + 1
if self.numberOfKeyPresses < 10 {
proxy.deleteBackward()
}
else {
if let documentContext = proxy.documentContextBeforeInput as NSString? {
let tokens : [String] = documentContext.componentsSeparatedByString(" ")
var i : Int = Int()
for i = 0; i < String(tokens.last!).characters.count + 1; i++ {
(self.textDocumentProxy as UIKeyInput).deleteBackward()
}
}
else {
print("proxy.documentContextBeforeInput was nil")
self.keyRepeatTimer.invalidate()
self.numberOfKeyPresses = 0
}
}
}
else {
print("In the outer else")
self.keyRepeatTimer.invalidate()
self.numberOfKeyPresses = 0
}
}
最后,我不完全理解为什么,但是当我创建键盘扩展时,XCode会自动在下面插入这两个函数。为了试图让它发挥作用,我稍微修改了它们。
override func textWillChange(textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
super.textWillChange(textInput)
}
override func textDidChange(textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
//let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
super.textDidChange(textInput)
}
答案 0 :(得分:2)
让我们来描述下面这一行:
if !((self.textDocumentProxy.documentContextBeforeInput?.isEmpty) == nil) {
首先,它需要一个标记为optional
的对象(?
字母):
let documentContext = self.textDocumentProxy.documentContextBeforeInput
然后,它尝试读取它的名为isEmpty
的属性:
let isEmpty = documentContext?.isEmpty
然后评估结果:
if !(isEmpty == nil) {
有两个错误。第一个是您将Bool
值与nil
进行比较。另一个原因是您不确定documentContext
不是nil
。
所以,让我们以更合适的方式编写代码:
if let documentContext = self.textDocumentProxy.documentContextBeforeInput { // Make sure that it isn't nil
if documentContext.isEmpty == false { // I guess you need false?
// Do what you want with non-empty document context
}
}