2017-09-21 57 views
0

之間的標籤這是我的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設置標籤,但在這裏你缺少正確的語法,該numberLabelUILabel例如,如果你要設置的文字標籤上有String類型的屬性text你必須指定一些字符串這樣的:

self.numberInCircleView.numberLabel.text = "5" 
0

I'm not sure how I call between views to do this 你可以調用任何對象的任何方法,直到你不得不對它的引用。

self.numberInCircleView.numberLabel = 5這是編譯時錯誤,因爲你不能分配5型 「的UILabel」

的更換 self.numberInCircleView.numberLabel = 5

self.numberInCircleView.numberLabel.text = "5"