从Swift中的另一个类访问ViewController中的变量

时间:2016-02-11 17:24:18

标签: ios swift class

我在ViewController中有一个滑块sliderLineSize和一个变量lineSize。 UISlider sliderLineSize更改lineSize。但是,lineSize实际上用于附加到UIView的drawRect类的viewLine部分。

问题:

如何将ViewController中设置的变量lineSize传递给viewLine中的drawRect使用的变种import UIKit class ViewController: UIViewController { @IBOutlet weak var myView: UIView! @IBOutlet weak var myImageView: UIImageView! var lineSize: Int = 1 override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { myImageView.alpha = 0.5 } @IBAction func sliderLineSize(sender: UISlider) { lineSize = Int(sender.value) } } class viewLine: UIView { let path=UIBezierPath() var incrementalImage:UIImage? var previousPoint:CGPoint = CGPoint.zero var strokeColor:UIColor? required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override func drawRect(rect: CGRect) { incrementalImage?.drawInRect(rect) path.lineWidth = lineSize path.stroke() } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch: AnyObject? = touches.first let currentPoint = touch!.locationInView(self) path.moveToPoint(currentPoint) previousPoint=currentPoint self.setNeedsDisplay() } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { let touch: AnyObject? = touches.first let currentPoint = touch!.locationInView(self) let midPoint = self.midPoint(previousPoint, p1: currentPoint) path.addQuadCurveToPoint(midPoint,controlPoint: previousPoint) previousPoint=currentPoint path.moveToPoint(midPoint) self.setNeedsDisplay() } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { self.drawBitmap() self.setNeedsDisplay() path.removeAllPoints() } func midPoint(p0:CGPoint,p1:CGPoint)->CGPoint { let x=(p0.x+p1.x)/2 let y=(p0.y+p1.y)/2 return CGPoint(x: x, y: y) } func drawBitmap() { UIGraphicsBeginImageContextWithOptions(self.bounds.size, true, 1) strokeColor?.setStroke() if((incrementalImage) == nil){ let rectPath:UIBezierPath = UIBezierPath(rect: self.bounds) UIColor.whiteColor().setFill() rectPath.fill() } incrementalImage?.drawAtPoint(CGPointZero) path.stroke() incrementalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } } < / p>

npm install babel-plugin-transform-class-properties  --save-dev

2 个答案:

答案 0 :(得分:3)

有两种主要方法可以做到这一点。

选项1:

为您的ViewLine课程提供自己的lineSize媒体资源:

class ViewLine: UIView {
    var lineSize = 1
}

ViewController提供ViewLine,并使用属性观察者更新viewLine内的ViewController内的属性:

class ViewController: UIViewController {

    // v~~~ be sure to connect this outlet in interface builder
    @IBOutlet weak var viewLine: ViewLine!

    var lineSize = 1 {
        didSet {
            viewLine.lineSize = lineSize
        }
    }
}

现在,您的ViewLine类将拥有自己的lineSize属性,可以直接在其drawRect方法中进行访问。

选项2:

ViewLine课程提供ViewController的参考:

class ViewLine: UIView {

    // v~~~ be sure to connect this outlet in interface builder
    @IBOutlet weak var controller: ViewController!
}

现在,在drawRect方法中,将path.lineWidth = lineSize替换为path.lineWidth = controller.lineSize

基本上,你的一个类需要引用另一个类才能使它们能够进行通信。

答案 1 :(得分:1)

你应该制作Singleton Model类。可以从任何地方访问单例类。这是你应该如何在swift中创建一个单例类。

class ApplicationModel {

    class var sharedInstance: ApplicationModel {
        get {
            struct Static {
                static var instance: ApplicationModel? = nil
                static var token: dispatch_once_t = 0
            }
            dispatch_once(&Static.token, {
                Static.instance = ApplicationModel()
            })
            return Static.instance!
        }
    }

    var lineSize = 1
}

内部ViewController

override func viewDidLoad() {
        super.viewDidLoad()

        //Instantiate ApplicationModel
        //GET
        let lineSize = ApplicationModel.sharedInstance.lineSize

        //SET
        ApplicationModel.sharedInstance.lineSize = 5
    }

在viewLine内部

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

     //Access Application Model


            //GET
            let lineSize = ApplicationModel.sharedInstance.lineSize

            //SET
            ApplicationModel.sharedInstance.lineSize = 5
}

希望这有帮助!