我有两个* .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