ios - 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)
}
}
解决方案
为 PickViewController 类创建对象并将其委托设置为 self。
var yourObj = PickViewController()
override func viewDidLoad() {
super.viewDidLoad()
yourObj.delegate = self
}
推荐阅读
- visual-studio - Google Play 应用签名密钥与 Visual Studio
- django - 在 Djnago 中进行迁移不会创建 ForeignKey 和 OneToOneFields,而是在创建模型后添加它们。无法迁移
- node.js - 使用 JWT (Node.js + mongoose) 在 REST API 中管理用户权限的最佳方式
- javascript - 多个 div 元素的 JS 切换
- c++ - 从文本文件中读取尺寸和表格内容后显示表格
- c - C TCP/IP 套接字:如何将来自客户端套接字的输入同步到服务器套接字以交替进行
- python - 应用程序生成的问题数量取决于他/她在购物车应用程序中的票数。我怎样才能使这项工作?
- javascript - 电子在页面上保存用户更改
- swift - DispatchQueue 不会快速更新数据
- java - 方法 flatMapping((
disc) -> {}, toSet()) 未为 Grouping 类型定义