我怎样才能使用函数等待在 swift 3 中得到一些东西?
how can I use function waiting to get something in swift 3?
我想使用一种等待获得某些东西然后做其他事情的方法 - 我的应用程序中有一个渐进式并且想要使用一个等待渐进式达到 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 answer for delegates. is an example for notifications. And 答案包含如何使用完成处理程序块 它们在此处有详细说明。
我正在用委托示例更新此内容:
//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 或任何 class 中您想接收事件(假设它是 ViewController.swift),
在ViewController.swift
class ViewController: UIViewController, delegateForEvent { // Set delegateForEvent in interface
在 viewController.swift
中设置创建事件的委托ViewController 实例
myEventViewController.delegate = self; // set it where you created your object
并在viewController.swift
中添加委托函数
didEventCompleted{
//here you can do whatever you want to after the event completion
}
我想使用一种等待获得某些东西然后做其他事情的方法 - 我的应用程序中有一个渐进式并且想要使用一个等待渐进式达到 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 answer for delegates.
我正在用委托示例更新此内容:
//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 或任何 class 中您想接收事件(假设它是 ViewController.swift),
在ViewController.swift
class ViewController: UIViewController, delegateForEvent { // Set delegateForEvent in interface
在 viewController.swift
中设置创建事件的委托ViewController 实例myEventViewController.delegate = self; // set it where you created your object
并在viewController.swift
中添加委托函数didEventCompleted{
//here you can do whatever you want to after the event completion
}