我有兩個* .swift文件。一個是帶滑塊的ViewController。 第二個是「抽屜」類,它繪製了一條線。 (1)從滑塊值中獲取一個變量,然後(2)將其傳遞給'drawer'來繪製一條線,然後(3)通知滑塊它可以傳遞一個新值來重繪線。在兩個* .swift文件之間傳遞數據而無需prepareForSegue
我應該使用委託來傳遞數據還是它太複雜了? 不幸的是,我無法找到沒有prepareForSeague
的委託方法。
代碼如下。
的ViewController:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBAction func slider(_ sender: UISlider) {
label.text = String (sender.value)
}
drawer.swift:
import UIKit
// var position = 300 - Here I want to get a variable from the ViewController
class drawer: UIView {
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context?.setLineWidth(2.0)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let components: [CGFloat] = [0.0, 0.0, 1.0, 1.0]
let color = CGColor(colorSpace: colorSpace, components: components)
context?.setStrokeColor(color!)
context?.move(to: CGPoint(x: 30, y: 30))
context?.addLine(to: CGPoint(x: position, y: 400))
context?.strokePath()
}
}
https://stackoverflow.com/a/41560949/2303865 –
從哪裏抽屜傳遞值類。迅速? –
一旦滑塊的值發生變化,就必須將其發送到抽屜以重新繪製線條。 – user3820453