不调用方法的协议和委托模式

Protocol and delegate pattern not calling the method

按照专家@DonMag 的建议实施 protocol/delegate 模式,我无法使代码正常工作...

我显示如下评分 window 当用户更改评分时,我希望评分表情符号更新并弹出控制器,我也使用动画,现在查看我有一个单独的评分class 和控制器一个单独的 class,

问题是这个函数被窃听了,我可以检测到它但它没有达到协议方法

@objc func ratingButtonTapped(_ sender: UIButton) {
      //  print(sender.titleLabel?.text)
        guard let t = sender.titleLabel?.text else {
            return
        }
        
        ratingDelegate?.defineTheratings(t)
                 
    }

控制器class

import UIKit
import CoreData

protocol RatingsPresentation: class {
    func defineTheratings(_ ratings: String)
}

class RatingsViewController: UIViewController, RatingsPresentation {
    func defineTheratings(_ ratings: String) {
         print("ratings seleced\(ratings)")
     
               self.restaurant.rating = ratings
               if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
                   appDelegate.saveContext()
               }
     
               
               if  self.presentedViewController  != nil {
                   self.dismiss(animated: true, completion: nil)
               }
               else {
                   self.navigationController?.popViewController(animated: true)
               }
    }

    var restaurant: Restaurant!
    var rates = RatingsView()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(rates.view)
        //Delegates
        let vc = RatingsView()
        // set the rating delegate
        vc.ratingDelegate = self
        //
        if let restaurantImage = restaurant.image {
            
            rates.bkgImageView.image = UIImage(data: restaurantImage as Data)
        }
        
        rates.crossBtn.addTarget(self, action: #selector(closeRatings), for: .touchUpInside)
  
        
        let animateCross = CGAffineTransform(translationX: 1000, y: 0)
        rates.crossBtn.transform = animateCross
  
    }
    
    @objc func closeRatings() {
    
        navigationController?.popViewController(animated: true)
     
    }

}

查看Class

import UIKit

class RatingsView: UIViewController {
    
    var bkgImageView = UIImageView()
    var crossBtn = UIButton()
    var btnCollection: [UIButton] = []
    weak var ratingDelegate: RatingsPresentation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let ratings: [String] = ["cool", "happy","love", "sad", "angry"]
        
        let animationBlur = UIBlurEffect(style: .dark)
        let visualBlurView = UIVisualEffectView(effect: animationBlur)
        
        
        
        
        // add elements to self
        view.addSubview(bkgImageView)
        view.addSubview(visualBlurView)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        visualBlurView.translatesAutoresizingMaskIntoConstraints = false
        bkgImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        bkgImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        bkgImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        bkgImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        // constrain visualBlurView to all 4 sides
        visualBlurView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        visualBlurView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        visualBlurView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        visualBlurView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        bkgImageView.contentMode = .scaleAspectFill
        // Do any additional setup after loading the view.
        //Add stackView
        
        let stackEmoji = UIStackView()
        stackEmoji.translatesAutoresizingMaskIntoConstraints = false
        
        stackEmoji.axis = .vertical
        stackEmoji.spacing = 5
        stackEmoji.distribution = .fill
        stackEmoji.alignment = .top
        
        let font = UIFont(name: "Rubik-Medium", size: 28)
        let fontM = UIFontMetrics(forTextStyle: .body)
        
        ratings.forEach { (btn) in
            let b = UIButton()
            b.setTitle(btn, for: .normal)
            b.titleLabel?.font = fontM.scaledFont(for: font!)
            b.setImage(UIImage(named: btn), for: .normal)
            stackEmoji.addArrangedSubview(b)
            btnCollection.append(b)
            
            //btn animation
            let sizeAnimation = CGAffineTransform(scaleX: 5, y: 5)
            let positionAnimation = CGAffineTransform(translationX: 1000, y: 0)
            let combinedAninaton = sizeAnimation.concatenating(positionAnimation)
            b.transform = combinedAninaton
            b.addTarget(self, action: #selector(ratingButtonTapped(_:)), for: .touchUpInside)
        }
        
        view.addSubview(stackEmoji)
        
        stackEmoji.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        stackEmoji.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        
//          if let img = UIImage(named: "cross") {
//            crossBtn.setImage(img, for: [])
//              }
        
        crossBtn.setTitle("X", for: [])
        crossBtn.setTitleColor(.white, for: .normal)
        crossBtn.setTitleColor(.gray, for: .highlighted)
        crossBtn.titleLabel?.font = UIFont.systemFont(ofSize: 44, weight: .bold)
        view.addSubview(crossBtn)
        
        
        crossBtn.translatesAutoresizingMaskIntoConstraints = false
        crossBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
        crossBtn.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
        
       

    }
    
  
    
    @objc func ratingButtonTapped(_ sender: UIButton) {
      //  print(sender.titleLabel?.text)
        guard let t = sender.titleLabel?.text else {
            return
        }
        
        ratingDelegate?.defineTheratings(t)
                 
    }
   

}

问题在这里

var rates = RatingsView()  //// shown 1 
override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(rates.view)  /////// you add this as a subview
    //Delegates
    let vc = RatingsView()       //////// this is useless - not shown 1
    // set the rating delegate
    vc.ratingDelegate = self   ////// and set the delegate for this non-shown  

因此您需要删除 let vc = RatingsView() 并为

设置委托
rates.ratingDelegate = self