简体   繁体   English

当UI更新时,IOS会收到通知。

[英]IOS get notified when UI updates.

I have a custom keyboard extension. 我有自定义键盘扩展名。 This function is called when the delete key is pressed: 按下删除键时调用此函数:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
            {
                for _ in 1..<50
                {
                    (self.textDocumentProxy as UIKeyInput).deleteBackward()
                }
                print("Deletion End")
                self.deleteCounter = 0
        })

I do not think the dispatch_async is relevent but I included it, just incase. 我不认为dispatch_async是相关的,但我把它包括在内,只是因为它。

The problem is that even though my console prints "Deletion End" once the loop is finished, the UI of the textfield does not update until a second or two has passed. 问题是,即使我的控制台在循环结束后打印“Deletion End”,文本字段的UI也不会更新,直到一两秒钟过去。

It seems calling 好像在呼唤

(self.textDocumentProxy as UIKeyInput).deleteBackward()

Does not immediately delete a character and update the UI. 不立即删除字符并更新UI。

How can I be notified when the UI is actually updated? 如何在实际更新UI时通知我?

Change like this: 改变如下:

dispatch_async(dispatch_get_main_queue(),{
                for _ in 1..<50
                {
                    (self.textDocumentProxy as UIKeyInput).deleteBackward()
                }
                print("Deletion End")
                self.deleteCounter = 0
        })

Explanation: 说明:

The UI must work in the Main thread so when you work with the background queue you always have to dispatch in main queue the UI updates. UI必须在主线程中工作,因此当您使用后台队列时,您始终必须在主队列中分派UI更新。

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

相关问题 iOS推出应用更新。需要DB更新时保持用户数据完好无损 - iOS Rolling out app updates. Keeping user data intact when DB update required 自定义键盘:文本更新时得到通知 - Custom keyboard: get notified when text updates iOS:在返回UITableViewCell时获得通知 - iOS: Get notified when UITableViewCell is returned 在通讯录更新时收到通知 - getting notified when addressbook updates 在iOS中连接或断开wifi时如何获取通知? - How to get notified when wifi gets connected or disconnected in iOS? iOS 当呈现的视图控制器被取消时在父视图控制器中得到通知 - iOS get notified in parent viewcontroller when presented viewcontroller is dimissed 当 UIViewController 返回顶部时,iOS 中是否有委托函数来获得通知? - Is there a delegate function in iOS to get notified when a UIViewController is coming back to top? iOS - 如何在AFHTTPClient完成所有操作时收到通知 - iOS - How to get notified when AFHTTPClient is done with all operations 当用户允许iOS中的摄像头访问警报时如何获得通知? - How to get notified when user allows the camera access alert in iOS? 如何在iOS中卸载我们的应用程序时收到通知 - How to get notified when our app is uninstalled in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM