I have the following function that runs whenever a button is pressed:
func deletekeyPressed(sender: UIButton!) {
while(textDocumentProxy.hasText()) {
(textDocumentProxy as UIKeyInput).deleteBackward()
}
for _ in 1..<2000 {
(textDocumentProxy as UIKeyInput).deleteBackward()
}
}
To prevent user from thinking the program is crashing I want the text of the delete button which normally says "Delete All" to "Deleting..." or even better have the title alternate between the following states: "Deleting", "Deleting.", "Deleting..", "Deleting..."
So far I have tried putting a setTitle()
for UIControlState.Normal
at the beginning and end of the function in order to make the text say "Deleting" when the function is running.
This makes my function look like this:
func deletekeyPressed(sender: UIButton!) {
sender.setTitle("Deleting...", forState: .Normal)
while(textDocumentProxy.hasText()) {
(textDocumentProxy as UIKeyInput).deleteBackward()
}
for _ in 1..<2000 {
(textDocumentProxy as UIKeyInput).deleteBackward()
}
sender.setTitle("Deleting...", forState: .Normal)
}
However, nothing is happening. How do I implement this solution?
Update One: I implemented Aaron's solution like this:
func deletekeyPressed(sender: UIButton!) {
NSLog("-------------------")
NSLog("Pressing delete key")
sender.setTitle("Deleting...", forState: .Normal)
sender.userInteractionEnabled = false
dispatch_async(dispatch_get_main_queue()) {
NSLog("Starting delete function")
while(self.textDocumentProxy.hasText()) {
NSLog("Deleting 3 times")
(self.textDocumentProxy as UIKeyInput).deleteBackward()
(self.textDocumentProxy as UIKeyInput).deleteBackward()
(self.textDocumentProxy as UIKeyInput).deleteBackward()
}
for _ in 1..<2000 {
(self.textDocumentProxy as UIKeyInput).deleteBackward()
}
sender.setTitle("Clear", forState: .Normal)
sender.userInteractionEnabled = true
NSLog("Finishing delete function")
}
However I run into the following problem. The solution partially works in the sense that the button text will say "Deleting..." until the code inside dispatch_async
finishes running. But the problem is the UI of textfield in which the text is being deleted is not updated as soon as the code inside dispatch_async
finishes running. This leads the delete button's text to say "Deleting..." for a short amount of time (as long as it takes for the code to finish running) even if the UITextfield has not updated itself to show the change from the code.
Aucun commentaire:
Enregistrer un commentaire