我在我的應用程序中有2個ViewController。其中一個名爲vcA的方法是使用一個方法(這裏是viewDidLoad)與我的網絡層(我的應用程序中的一個類)交談。在完成聯網工作(將由一個完成處理程序推斷)後,我想通知類vcB調用一個方法來獲取由網絡層提供的一些數據。 請大家看看下面須藤代碼:Swift:從另一個使用完成處理程序通知ViewController
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)
}
}
使用NSNotifications – Paulw11
謝謝。我在這裏閱讀了這篇文章,但我不明白有什麼區別B/W NSNotifications&NSNotificationCenter&performSelector&KVO,哪一個最適合我的情況? –