以下のコードスニペットに示すようにシングルトンクラスを持っています。私はswift3で委譲者としてシングルトンクラスを作成するにはどうすればいいですか
protocol EmpLoginDelegate {
func empLoginSuccess()
func empLoginFailed()
}
class CommunicationModule {
static let sharedInstance = CommunicationModule()
private init() {
}
var empLoginDelegate:EmpLoginDelegate?
func test(){
self.empLoginDelegate?.empLoginSuccess()
}
}
私の委任クラスは以下のコードスニペットに示されています。
extension LoginViewController: EmpLoginDelegate{
func empLoginSuccess(){
wrongAttempts = 0
loginSuccess = true
print("EmpLoginIsSuccess")
performSegue(withIdentifier: "attendanceView", sender: self)
}
func empLoginFailed(){
wrongAttempts = wrongAttempts + 1
userNameTextField.shake(count: 3, for: 0.3, withTranslation: 10)
passwordTextField.shake(count: 3, for: 0.3, withTranslation: 10)
loginSuccess = false
loginAlert(alertTitle: "Invalid Credentials", alertMsg: "Your employee id or password is not correct")
}
}
私はemploginSuccess(テスト機能を呼び出す)メソッドが呼び出されません。テスト機能は、エラーなしで正常に実行されます。 問題はempLoginDelegateは私のコードでは初期化されていないと思ったので、自己として初期化する方法を試しましたが、何も私のために働いていませんでした。 swift3(iOS 10.3.1)のシングルトンクラスで委譲パターンを使用する他の方法はありますか?
'LoginViewController'の内部メソッドから' CommunicationModule.sharedInstance.empLoginDelegate = self'を設定しようとしましたか? – vacawama
速い応答のためにあなたのvacawamaに感謝し、その問題を解決しました。 –
ようこそ。私はそのコメントを答えに移しました。 – vacawama