ios - 我如何使用功能等待在 swift 3 中获取某些东西?

标签 ios swift3 listener wait

我想使用一种等待获得某些东西然后做其他事情的方法 - 我的应用程序中有一个渐进式并且想使用一个等待渐进式达到 100% 然后该函数执行的函数 - 在一般来说,我想知道如何使用一种在其他事情发生时执行的方法——我多次使用组方法,但这更不同(它有点像监听器)

let myGroup = DispatchGroup()

myGroup.enter()
//// Do your task
//// When you task complete
myGroup.leave()
myGroup.notify(queue: DispatchQueue.main) {              
////// do your remaining work         
}

最佳答案

您可以使用notificationdelegateblocks 来做到这一点。检查代表的 this 答案。 Here 是通知的示例。而 this 答案包含如何使用完成处理程序 block ,它们在此处有详细说明。

我正在用委托(delegate)示例更新此内容:

//Do this in the class (assuming it as EventViewController) where event occurs


    protocol delegateForEvent {
    func didEventCompleted()
     }

class EventViewController: UIViewController {
var delegate:delegateForEvent!

当事件完成时,调用这个

self.delegate.didEventCompleted()   // call this at the time of color change

然后在 ViewController 或任何你想接收事件的类中(假设它是 ViewController.swift),

ViewController.swift

class ViewController: UIViewController, delegateForEvent {  // Set delegateForEvent in interface

在 viewController.swift 中设置您创建 eventViewController 实例的委托(delegate)

myEventViewController.delegate = self;   // set it where you created your object

并在viewController.swift中添加委托(delegate)函数

didEventCompleted{
   //here you can do whatever you want to after the event completion
}

关于ios - 我如何使用功能等待在 swift 3 中获取某些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275724/

相关文章:

ios - IOS中的POST方法问题

ios - 多个 Xcode 项目将不再在真实的 iOS 设备上成功编译

ios - 在 Swift 迁移后使用未解析的标识符 'enumerate'

android - AudioFocusChangeListener 无法收听

iphone - UIbutton 标签属性

ios - 在 iOS 中,如何在 Swift 中的数字键盘上方添加一行?

swift - 单击该行后,UITableview 中的 subview 就会消失。

ios - swift3 中的选择器

android - 从 Android Wear 向手机上的后台服务发送消息

java - 使用 Spring boot 在 Listener 中调用使用 @RequestScope 注解的类