我的应用中有2个ViewControllers。其中一个名为vcA的方法是使用一种方法(此处为viewDidLoad)与我的网络层(我的应用程序中的一个类)进行通信。在完成网络作业(将由完成处理程序推断)后,我想通知类vcB调用一个方法来获取网络层提供的一些数据。 请看下面的sudo代码:
class Networking {
static var PublicValue : SomeKindOfClass? = nil
static func test(completionHandler : (successful : Bool) -> Void) -> Void {
//Do some networking in background
Network.BackgroundNetworking() {
if result = true {
PublicValue = SomeValue
completionHandler(successful : true)
}
else {
completionHandler(successful : false)
}
}
}
class vcA : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
Networking.test(completionHandler : { (successful) in
if successful == true {
//Here I want to notify class vcB to call printPublicValue method
}
})
}
}
class vcB : UIViewController {
func printPublicValue() {
print(Networking.PublicValue)
}
}
答案 0 :(得分:0)
在最新的 swift 版本 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "Name"), object: notification)
和