-2
我有兩個viewControllers
:LoginViewController和NextViewController。現在,當應用程序運行時,LoginViewController運行一個完成處理程序,它負責使用HTTP請求進行身份驗證,並在完成之後調用另一個可獲取必要用戶數據的閉包。iOS Swift:在視圖和完成處理程序之間傳遞數據
我不知道如何將回調函數中返回的數據傳遞給NextViewController以顯示它,因爲我無法知道數據何時可用,因爲它在後臺運行HTTP請求。
那麼我應該如何提供數據時,它變得可用?
我知道我可以調用第二個回調方法來獲取nextViewController中的用戶信息,但這會讓應用程序變慢。
這裏的示例代碼:
class Methods: NSObject {
//Singleton
class func sharedInstance() -> Methods {
struct Singleton {
static var sharedInstance = Methods()
}
return Singleton.sharedInstance
}
private func GETMethod(callBackMethod: (Success: Bool) -> Void) {
//Do the authentication
}
private func retriveUserData(callBackMethod: (data: String, Success: Bool) -> Void) {
//Gets the data and passes processed data back in a callBackMethod
}
func doAuthentication(callBackMethod: (Success: Bool) -> Void){
GETMethod { (Success) in
if Success {
self.retriveUserData({ (data, Success) in
data // <- HOW DO I GET THIS INTO LOGIN VIEW CONTROLLER?
callBackMethod(Success: true)
})
}
}
}
}
class LoginViewController: UIViewController {
func loginButtonPressed(){
Methods.sharedInstance().doAuthentication { (Success) in
}
}
}
class NextViewController: UIViewController {
//Present data when it becomes available
}