我會建議一個委託模式,因爲回調更爲一次性,而委託更好地服務於持續幫助對象的生命週期的目的。無論如何,我已經創建了一個例子來應對你的要求,git它here(框架+測試應用程序包括)
它涉及一個LoginController,這是框架的主要入口點和管絃樂隊。
當你初始化它時,你會傳遞一個回調函數來發送事件,包括「忘記密碼」和「用戶想退出」,這些都是在枚舉中定義的。
public enum LoginFrameworkStatus {
case Login
case ForgotPassword
case Help
case Disaster
case Exited
case UserWantsExit
}
類提供了一個入口點啓動進程:
public func enterLoginWorkflow(on controller: UIViewController, callback: LoginFrameworkCallback) {
let myBundle = Bundle(for: LoginController.self)
if let navi = UIStoryboard(name: "LoginWorkflow", bundle: myBundle).instantiateInitialViewController() as? MySpecialNavigationController {
presentingController = controller
navi.loginController = self
self.callback = callback
controller.present(navi, animated: true, completion: {
//presented!
callback?(.Login, navi, self) //If reference to LoginController is lost, tell the callback there's a problem.. shouldn't happend because we have a strong reference on the navigation controller.
})
}
}
..和退出點:
public func leaveLoginWorkflow() {
presentingController?.dismiss(animated: true, completion: {
self.callback?(.Exited, nil,self)
})
}
因此,對於你的框架的主界面將是:
LoginController().enterLoginWorkflow(on: self) { (status, controller, loginController) in
print("\(status) in \(controller?.description ?? "No Controller")")
switch status {
case .UserWantsExit:
loginController?.leaveLoginWorkflow()
case .ForgotPassword:
loginController?.leaveLoginWorkflow()
default:
()
}
}
在測試應用程序中我包含最低工作流程供您測試。
讓我知道這是你需要什麼,或者如果你想調查委託模式,我認爲這會更適合這個。
有人可以幫忙嗎? – Swamy
你試過[這](https://mobilefirstplatform.ibmcloud.com/tutorials/en/foundation/6.3/authentication-security/custom-authenticator-login-module/custom-authenticator-login-module-native-ios-應用程序/)已經? –