Home > Enterprise >  customs alertView removeFromSuperview and not found does not conform to expected type 'CastingP
customs alertView removeFromSuperview and not found does not conform to expected type 'CastingP

Time:06-02

I have some issues with my customs alertView when tried to compile it it will give me the following errors

Value of type 'CastingPromptViewDelegate' has no member

'removeFromSuperview' Argument type 'CastingPromptView' does not conform to expected type 'CastingPromptViewDelegate'

on my watchVC I have this

private lazy var castingPromptView: CastingPromptView = {
    let view = CastingPromptView.instanceFromNib(castDeviceName: "test", imageURLString: "https://dsfsdf.jpg")
    view.translatesAutoresizingMaskIntoConstraints = false
    view.delegate = self
    return view
}()


//cast CastingPromptView 
    class CastingPromptView: UIView{
        // MARK: - IBOutlets
        @IBOutlet weak var castTODeviceLabel: UILabel!
        @IBOutlet weak var videoImage: URLImageView?
        @IBOutlet weak var playCastingButton: UIButton!
        @IBOutlet weak var stopCastingButton: UIButton!
        @IBOutlet weak var cancelButton: UIButton!
        
        // MARK: - Properties
        public var delegate: CastingPromptViewDelegate?
        
        class func instanceFromNib(castDeviceName:String,imageURLString:String) -> CastingPromptView {
            let view = UINib(nibName: "CastingPromptView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CastingPromptView
            view.castTODeviceLabel.text = castDeviceName
    
            view.setupView()
            return view
        }
        
        override init(frame: CGRect) {
          super.init(frame: frame)
    
        }
        
        //initWithCode to init view from xib or storyboard
        required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          setupView()
        }
        
        private func setupView() {
     
            self.backgroundColor = .white
            self.layer.borderColor = UIColor(red: 0.98, green: 0.98, blue: 0.98, alpha: 1.00).cgColor
            self.layer.borderWidth = 1
            self.layer.cornerRadius = 12
        }
        
        @IBAction func dismissView(_ sender: Any) {
            
          self.delegate?.dismissAlert(sender: self) ->> conform to expected type 'CastingPromptViewDelegate'
        }
        
        @IBAction func stopCasting(_ sender: Any) {
            
        }
        
        @IBAction func playCasting(_ sender: Any) {
    
        }
     }
    
    // MARK: - Delegate
    protocol CastingPromptViewDelegate {
        func dismissAlert(sender: CastingPromptViewDelegate)
    
    }


extension WatchVC:CastingPromptViewDelegate {
    func dismissAlert(sender: CastingPromptViewDelegate) {
        sender.removeFromSuperview() -->Value of type 'CastingPromptViewDelegate' has no member 'removeFromSuperview'
        self.backgroundView.removeFromSuperview()
        
    }

CodePudding user response:

Try this out:

protocol CastingPromptViewDelegate {
    func dismissAlert(sender: CastingPromptView) // instead of CastingPromptViewDelegate
}

Then your extension will be:

extension WatchVC:CastingPromptViewDelegate {
    func dismissAlert(sender: CastingPromptView) {
        sender.removeFromSuperview()
        self.backgroundView.removeFromSuperview()
    }
}
  • Related