我想使用一种等待获得某些东西然后做其他事情的方法 - 我的应用程序中有一个渐进式并且想使用一个等待渐进式达到 100% 然后该函数执行的函数 - 在一般来说,我想知道如何使用一种在其他事情发生时执行的方法——我多次使用组方法,但这更不同(它有点像监听器)
let myGroup = DispatchGroup()
myGroup.enter()
//// Do your task
//// When you task complete
myGroup.leave()
myGroup.notify(queue: DispatchQueue.main) {
////// do your remaining work
}
最佳答案
您可以使用notification
、delegate
或blocks
来做到这一点。检查代表的 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/