Swift Logging for Custom Components

I have one UIViewControllerand UIView(as a component). Below is the code for the component.

class ProcessingProgressIndicator: UIView {

   var progressView: UIProgressView!

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

Therefore, I use this component in several controllers. Therefore, when I need to change the value of the progress that I used, as shown below in my controller.

 myProgressView.changeProgress(progress)

So, to add the Component Protocol Oriented component below to the code.

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 the method below

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

This is how I made it protocol oriented.

So my question is: Is the implementation method correct?

I need to pass a progressView object, can I get rid of it?

+4
source share
3

, Component Protocol Oriented .

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)

.

+4

, .

- Apple, , , . , "", , .

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
    }
}

Note that if an area updateProgressValueupdates the progress value in real time, then the delegate method customComponentProgressDidStartUpdatemust also be executed as real.

Alternatively, you can check this question / answer to find out more about what's going on here.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1669780/


All Articles