I got a problem. Given I have a UITextField an a button to delete the content of the text field.
So this works as expected:
- Enter text with keyboard
- tap on delete button
- tap with three fingers and select undo from the context menu
- text re-appears
Now things start to get weird when using dictation:
- tap the dictation button
- say some words so they appear in the text field
- tap the delete button
- tap with three fingers and select undo from the context menu
- App crashes
The crash is Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'
So it seems that the text from the dictation does not end up in the list of possible undos. Does anyone havens idea on how to solve this? What I tried so far:
- Clean app with basic code to verify (see code at the end of this post)
- Creating a subclass of UITextField to override
func dictationRecordingDidEnd()
and registering new undo with the text - scratched my head and wondering why
I would be very happy to have a solution here!
Code Example for testing app:
import UIKit
class ViewController: UIViewController {
let textField = UITextField(frame: CGRect(x: 50, y: 50, width: 200, height: 44))
override func viewDidLoad() {
super.viewDidLoad()
textField.layer.borderColor = UIColor.black.cgColor
textField.layer.borderWidth = 1
view.addSubview(textField)
let deletebutton = UIButton(type: .custom)
deletebutton.frame = CGRect(x: 50, y: 100, width: 100, height: 50)
deletebutton.addTarget(self, action: #selector(deleteText(_:)), for: .touchUpInside)
deletebutton.setTitle("delete", for: .normal)
deletebutton.setTitleColor(.black, for: .normal)
view.addSubview(deletebutton)
}
@objc func deleteText(_ sender: UIButton) {
textField.text = nil
}
}
Update 1
I now added the clear button to the text field by setting textField.clearButtonMode = .whileEditing
and this works as expected.
So the main question is: What does the built in clear button different than a custom one? Is some notification fired? Some other magic? Please enlighten me!
Update 2
The app also crashes when doing this:
- dictate something
- replace text with different one by using
textField.text = "Something"
- do a three finger tap and select undo
Since there where no answers yet, I will answer it as I found a solution.
I can bot answer why this happens but it seems to be pretty common that this behavior crashes apps which are widely used and available in the App Store.
Anyway, the following code will prevent a crash and is based on the answer listed here :
I'm happy to help anyone else with this answer!