2017-04-22 6 views
0

以下のコードスニペットに示すようにシングルトンクラスを持っています。私は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)のシングルトンクラスで委譲パターンを使用する他の方法はありますか?

+1

'LoginViewController'の内部メソッドから' CommunicationModule.sharedInstance.empLoginDelegate = self'を設定しようとしましたか? – vacawama

+0

速い応答のためにあなたのvacawamaに感謝し、その問題を解決しました。 –

+0

ようこそ。私はそのコメントを答えに移しました。 – vacawama

答えて

1

最良の方法である私にとって、良いと思います。 empLoginDelegateようLoginViewControllerのインスタンスを設定するには、呼び出し:

CommunicationModule.sharedInstance.empLoginDelegate = self 

LoginViewControllerの内部メソッドから。

1

私はこの方法では、それはあなたが適切シングルトンと通信していることを確認します

final class Singleton { 

    // Can't init is singleton 
    private init() { } 

    //MARK: Shared Instance 

    static let shared: Singleton = Singleton() 

} 
関連する問題