这是在我的viewController
中import UIKit
import SnapKit
class ViewController: UIViewController {
var numberInCircleView: NumberInCircleView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.setupViews()
}
func setupViews() {
self.numberInCircleView = NumberInCircleView(frame: .zero)
self.view.addSubview(self.numberInCircleView)
self.numberInCircleView.snp.makeConstraints { (make) in
make.leading.equalTo(20)
make.bottom.equalTo(-40)
make.width.equalTo(60)
make.height.equalTo(60)
}
self.numberInCircleView.numberLabel = 5
}
}
以下是我的自定义类。
import Foundation
import UIKit
class NumberInCircleView: UIView {
var numberLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
self.setupViews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setupViews()
}
func setupViews() {
self.numberLabel = UILabel(frame: .zero)
self.addSubview(self.numberLabel)
self.numberLabel.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
self.layer.cornerRadius = 30
self.backgroundColor = UIColor.red
self.layer.borderWidth = 1
self.layer.borderColor = UIColor.black.cgColor
self.numberLabel.text = ""
self.numberLabel.textAlignment = .center
self.numberLabel.textColor = UIColor.white
self.numberLabel.adjustsFontSizeToFitWidth = true
self.numberLabel.minimumScaleFactor = 0.5
}
我的ViewController中的这一行
self.numberInCircleView.numberLabel = 5
应将标签设置为5.我不确定如何在视图之间调用来执行此操作?您能否提一些建议?一些在线教程为我提供了更多关于此的信息,我们也非常感激。
答案 0 :(得分:0)
似乎您想要将值5设置为标签但是在这里您缺少正确的语法,numberLabel
是UILabel
实例并且具有类型的属性text
String
如果要将文本设置为标签,则必须分配一些字符串,如下所示:
self.numberInCircleView.numberLabel.text = "5"
答案 1 :(得分:0)
I'm not sure how I call between views to do this
你可以在任何对象上调用任何方法,直到你引用它为止。
self.numberInCircleView.numberLabel = 5
这是编译时错误,因为您无法为" UILabel"
替换
self.numberInCircleView.numberLabel = 5
与
self.numberInCircleView.numberLabel.text = "5"