我有一個名爲colorChangerViewClass
的自定義類,從UIView
繼承更改次數爲textLabel:從不同的視圖
class colorChangerViewClass: UIView {
@IBOutlet weak var controller: ViewController!
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// I want to change a textLabel in the main ViewController based on the current finger position here!
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
// And here aswell!
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { ... }
func setColor(color: UIColor) {
self.backgroundColor = color
}
}
的touchesBegan
和touchesMoved
方法,我想在主視圖控制器改變textLabel
內(一個不同)基於當前手指位置。
什麼是建立兩個類之間溝通的最佳方式colorChangerViewClass
和ViewController
?
我不明白你答案的最後部分。 我在界面生成器中創建了「ColorChangerViewClass」的實例。不是以編程方式。 當試圖使用你的代碼Xcode可以理解地抱怨'colorChangerView'被無效重新聲明。 – Marmelador
如果您在界面構建器中設置此類,那麼您可以在ViewController中創建此視圖的Outlet,並將控制器屬性設置爲'viewDidLoad'。 –