首页 > 解决方案 > ios Swift 协议数据

问题描述

我不使用故事板。我想使用@objc 按钮操作发送协议数据。但是,发送的视图控制器不运行协议功能。我可以知道是什么原因吗?事实上,还有很多代码。其他的工作,但只有协议功能不执行。didUpdataChampion 函数是将数据导入不同的协议。我已经确认这没有问题。

protocol MyProtocolData {
    func protocolData(dataSent: String)
    func protocolCount(dataInt: Int)
}

class PickViewController: UIViewController,ChampionManagerDelegate{
 
    static let identifier = "PickViewController"
    
    var count = 0
    var urlArray = [URL]()
    
    var pickDelegate : MyProtocolData?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        champions.riot(url: "myURL")
     }
    @objc func topHand(){
        pickDelegate?.protocolData(dataSent: "top")
        print(count)
        pickDelegate?.protocoCount(dataInt: count)
        let cham = ChampViewController()
        cham.modalPresentationStyle = .fullScreen
        present(cham, animated: true, completion: nil)
    }
 //Data imported to another protocol
    func didUpdataChampion(_ championManager: ChampionManager, champion: [ChampionRiot]) {
        print(#function)
        count = champion.count
        for data in champion {
            let id = data.id
            guard let url = URL(string: "https://ddragon.leagueoflegends.com/cdn/11.16.1/img/champion/\(id).png") else { return }
            urlArray.append(url)
            count = urlArray.count
        }

    }
    
    func didFailWithError(error: Error) {
        print(error)
    }
    
}
class ChampViewController: UIViewController,MyProtocolData {
    

    var pickData = ""
    var arrayCount = 0
 
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func protocolData(dataSent: String) {
        print(#function)
        pickData = dataSent
        print(pickData)
    }
    func protocoCount(dataInt: Int) {
        print(#function)
        arrayCount = dataInt
        print(arrayCount)
        
    }
}

标签: ios

解决方案


为 PickViewController 类创建对象并将其委托设置为 self。

var yourObj = PickViewController()
override func viewDidLoad() {
        super.viewDidLoad()
        yourObj.delegate = self
    }

推荐阅读