2017-02-15 16 views
4

UIViewControllerUIView(コンポーネントとして)があります。以下はコンポーネントのコードです。カスタムコンポーネントのSwiftでのプロトコル指向プログラミング

class ProcessingProgressIndicator: UIView { 

    var progressView: UIProgressView! 

    func changeProgress(_ progress: Float) { 
     progressView.progress = progress 
    } 
} 

私はこのコンポーネントを複数のコントローラで使用します。ですから、進歩の価値を変える必要があるときは、自分のコントローラーで以下のように使用しました。

myProgressView.changeProgress(progress) 

したがって、コンポーネント指向のコンポーネントを作成するために、私はコードに以下を追加しました。

protocol ProgressUpdateable { 
    func updateWith(progressView: ProcessingProgressIndicator,progress: Float) 
} 

extension ProgressUpdateable { 
    func updateWith(progressView: ProcessingProgressIndicator,progress: Float) { 
     // Method gets called and can change progress 
    } 
} 

は、だから私のコントローラから、私はこれは私がそれプロトコル指向作った方法です

updateWith(progressView: progressView,progress: progressData.progress) 

以下のようにメソッドを呼び出します。

私の質問は次のとおりです。正しい実装方法ですか?

私はそれを取り除くことができますprogressViewのオブジェクトを渡す必要がありますか?

+0

カスタムコンポーネントを作成し、プロトコルに準拠させてメソッドを呼び出す必要があります。混乱はどこですか? –

+0

プロトコルに準拠させる方法を説明できますか? –

+0

@parth Adrojaは、デリゲートに確認して、第1コントローラが第2コントローラを認識していることを確認してください。 –

答えて

4

したがって、プロトコル指向のコンポーネントを作成するために、私はコードに以下を追加しました。

protocol ProgressUpdateable { 
    func updateWith(progressView: ProcessingProgressIndicator,progress: Float) 
} 

extension ProgressUpdateable { 
    func updateWith(progressView: ProcessingProgressIndicator,progress: Float) { 
     // Method gets called and can change progress 
    } 
} 
So from my controller, I call method as below 

updateWith(progressView: progressView,progress: progressData.progress) 

これは私がプロトコル指向にした方法です。

0

あなたの話は、委任のためのプロトコルを使用していることです。

Thisは、アップルのドキュメントであり、非常によく編集されています。ここでは、プロトコルについてすべて説明しています。それをすべて読んだ上で、委任セッションにジャンプして、あなたが探しているものを正確に見てください。あなたの注意は、(例えばクロージャパラメータに進捗値を返すなど、他の選択肢が存在する)委任を経由して、それを達成することである場合

0

、それはのようになります。

protocol CustomComponentDelegate { 
    func customComponentProgressDidStartUpdate(component: UIView, progressValue: Float) 
} 

class CustomComponent: UIView { 

    var delegate:CustomComponentDelegate? 

    // ... 

    func updateProgressValue(progress: Float) { 
     progressView.progress = progress/100.0 
     progressLabel.text = "\(Int(progress)) %" 

     delegate?.customComponentProgressDidStartUpdate(component: self, progressValue: progressView.progress) 
     // or you might want to send (progress/100.0) instead of (progressView.progress) 
    } 

    // ... 
} 

私はカスタムと仮定しましたコンポーネントがUIViewのサブクラスなので、違いはありません。

使用:

class ViewController: UIViewController, CustomComponentDelegate { 
    //... 

    // or it might be an IBOutlet 
    var customComponent: CustomComponent? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //... 

     customComponent?.delegate = self 
    } 

    func customComponentProgressDidStartUpdate(component: UIView, progressValue: Float) { 
     // do whatever you want with the returned values 
    } 
} 

updateProgressValue範囲がリアルタイムとして進捗値を更新する場合、customComponentProgressDidStartUpdateデリゲート方法はまた、リアルタイムのように実行されるべきであること。

さらに、this question/answerをチェックして、ここで起こっていることを理解してください。

これが役に立った。

+0

実際、これは私が探しているのではない、あなたのケースは、コントローラが進歩から価値を取り戻すようなものです。私がしようとしているのは、コントローラがプロトコルを使用してコンポーネントに進捗値を送信し、コンポーネントが関連することを行うようなものです。あなたのケースは、実際に私が望むものと逆です。 –

+0

それで、あなたは 'updateProgressValue'がコントローラに存在することを意味しますか?それをcustomViewに送信したいのですか? –

+0

コンポーネント内にあり、object.methodとして使用していますが、他にもやりたいことがあります。 –

関連する問題