swift - 如何使用结构中的数据更改按钮的文本?
问题描述
所以我正在尝试使用我定义的结构更改 UICollectionView 单元格中按钮的文本,但我不知道该怎么做,有什么帮助吗?我已经尝试了一些事情,比如 didSet 并在那里更改了文本,但它没有用
struct customData {
var screenName: String
}
class orderViewController: UIViewController {
let data = [
customData(screenName: "Home"),
customData(screenName: "Search"),
customData(screenName: "Previous Orders"),
customData(screenName: "Profile")
]
}
class customCell: UICollectionViewCell {
var data: customData? {
didSet {
guard let data = data else {return}
tabBar.titleLabel?.text = data.screenName
}
}
fileprivate let tabBar: UIButton = {
let segueButton = UIButton()
segueButton.backgroundColor = .clear
segueButton.layer.cornerRadius = 15
segueButton.layer.cornerCurve = .continuous
segueButton.setTitle("Test", for: .normal)
segueButton.titleLabel?.font = FontKit.roundedFont(ofSize: 45, weight: .semibold)
return segueButton
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(tabBar)
tabBar.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
tabBar.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
tabBar.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
tabBar.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
tabBar.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
解决方案
修复它,在我的 didSet 函数中,我将函数更改为 tabBar.setTitle(data.screenName, for: .normal) 并设置它
推荐阅读
- python - 所有可能的矩阵组合
- django - 如何将域转发到 Django 网站?
- elasticsearch - 存储在字段中的数组的分页 - Elasticsearch
- python - 数组中的峰值检测 - numpy 维度的问题
- python - 在 Python 中对数组进行就地分区
- node.js - 无法对“FormData”执行“附加”:参数 2 不是“Blob”类型。角 12
- jasperserver - 如何在 jasper 中使用自定义角色
- javascript - 如何解决 JavaScript 舍入问题?
- php - 文件上传 PHP 在我的机器上工作正常,但对其他人来说,移动上传似乎无法正常工作,它是一个 wordpress 网站
- reactjs - 仅在以特定方式访问时在路由中显示反应组件