UIColorPickerController extension doesn't seem to work to set VC as delegate.

I am taking a reference to a VC in app delegate and using a UIColorPickerController extension with the following code, I'm setting this VC as a delegate to the picker. I need to take these lengthy steps because there is no reference to the picker that a UIColorWell displays!

This way, I can set the background color of the VC while the user selects different colors from picker, one after another. This is the give a live effect or color change.

However, I dont see this happening. The delegate is not being called.

Any help?

import Foundation
import UIKit

extension UIColorPickerViewController{
    open override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        delegate = appDelegate.addOrEditVC
    }
}```

Answered by Frameworks Engineer in 780017022

First off, what you are doing isn't an override, its a replace (and probably only works because UIColorPickerViewController is an Objective-C class). The super your calling is UIViewController.viewDidLoad() so your eliding anything that UIColorPickerViewController may be doing in viewDidLoad() by doing this.

Secondly, your assuming that other clients of UIColorPickerViewController are setting the delegate before your viewDidLoad() code gets called.

What you are doing is very dangerous. You do not know what all clients of UIColorPickerViewController maybe expecting, how UIColorPickerViewController is internally implemented, or what you may break now or in the future by doing this.

UIColorWell is a control and as such you can assign multiple actions to it. This is how you would monitor color changes to the color well and react to it immediately.

Accepted Answer

First off, what you are doing isn't an override, its a replace (and probably only works because UIColorPickerViewController is an Objective-C class). The super your calling is UIViewController.viewDidLoad() so your eliding anything that UIColorPickerViewController may be doing in viewDidLoad() by doing this.

Secondly, your assuming that other clients of UIColorPickerViewController are setting the delegate before your viewDidLoad() code gets called.

What you are doing is very dangerous. You do not know what all clients of UIColorPickerViewController maybe expecting, how UIColorPickerViewController is internally implemented, or what you may break now or in the future by doing this.

UIColorWell is a control and as such you can assign multiple actions to it. This is how you would monitor color changes to the color well and react to it immediately.

UIColorPickerController extension doesn't seem to work to set VC as delegate.
 
 
Q