Swift Playgrounds Class Viewcontroller没有会员“”?

时间:2018-03-24 14:11:44

标签: ios swift xcode viewcontroller swift-playground

我对快速编程相对较新并遇到了这个问题,老实说我不知道​​如何解决这个问题。我在游乐场有一个viewcontroller,并在该视图上有一个按钮。我有一个名为CanvasView的类,它有以下代码:

import UIKit
import PlaygroundSupport

public class CanvasView: UIView {

var lineColor: UIColor!
var lineWidth: CGFloat!

var path: UIBezierPath!
var touchPoint: CGPoint!
var startPoint: CGPoint!

public override func layoutSubviews() {
    setupDefaultSettings()
}

// control touches
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        startPoint = touch.location(in: self)
        path = UIBezierPath()
        path.move(to: startPoint)
    }
}

public override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        touchPoint = touch.location(in: self)
    }

    path.addLine(to: touchPoint)
    startPoint = touchPoint

    drawShapeLayer()
}

// Function to clear the drawn path
func clearCanvas() {
    guard let path = path else { return }
    path.removeAllPoints()
    layer.sublayers = nil
    setNeedsDisplay()
}

// draw shape on layer
public func drawShapeLayer() {
    guard let path = path else { return }
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = lineColor.cgColor
    shapeLayer.lineWidth = lineWidth
    shapeLayer.fillColor = UIColor.clear.cgColor

    layer.addSublayer(shapeLayer)
    setNeedsDisplay()
}

public func setupDefaultSettings() {
    lineColor = .black
    lineWidth = 5

    clipsToBounds = true
    isMultipleTouchEnabled = false
 }

}

正如您所看到的,该类有一个名为clearCanvas()的方法可以清除画布。在我的视图控制器中,我创建了一个继承自CanvasView类的对象,并尝试使用按钮调用clearCanvas函数但是这给了我一个类GameViewController没有成员drawView的错误?即使它显示它确实像10行以上。 GameViewController的代码:

import Foundation
import CoreML
import UIKit
import PlaygroundSupport

public class GameViewController : UIViewController {

public override func viewDidLoad() {
    super.viewDidLoad()

    //MARK: Variables and constants
    let yellowColor = UIColor(red:1.00, green:0.92, blue:0.23, alpha:1.0)
    var roundNumber = 1
    var currentScore = 0

    //MARK: Setting up the UI
    //Setting up the initial view
    let view = UIView()
    self.view = view
    view.backgroundColor = yellowColor

    //Adding the view which can be drawn on
    let drawView = CanvasView()
    drawView.backgroundColor = UIColor.white
    drawView.frame = CGRect(x: 12.5, y: 250, width: 350, height: 350)
    drawView.layer.cornerRadius = 20
    view.addSubview(drawView)

    //Adding a button to start the round
    let startRoundButton = UIButton()
    startRoundButton.frame = CGRect(x: 0, y: 50, width: 100, height: 50)
    view.addSubview(startRoundButton)

    startRoundButton.addTarget(self, action: #selector(startRoundButtonPressed), for: .touchUpInside)

    //Adding a button clear the canvas
    let clearCanvasButton = UIButton()
    let clearCanvasButtonImage = UIImage(named: "clearButton")
    clearCanvasButton.setImage(clearCanvasButtonImage, for: .normal)
    clearCanvasButton.frame = CGRect(x: 12.5, y: 620, width: 110, height: 27)
    view.addSubview(clearCanvasButton)

    clearCanvasButton.addTarget(self, action: #selector(clearCanvasButtonPressed), for: .touchUpInside)

    //MARK: Functions
    func clearDrawView() {
        drawView.clearCanvas()
    }

}

@objc func startRoundButtonPressed() {

}

@objc func clearCanvasButtonPressed() {
    GameViewController.clearDrawView()
 }

}

我确实复制了CanvasView类,所以也许有些东西搞砸了那里的方法调用。如果有人能帮助我解决这个(也许是非常愚蠢的)问题,我将非常感激,并记住这是在mac上的游乐场而不是xcode中制作的。万分感谢!

1 个答案:

答案 0 :(得分:1)

您当前的clearDrawView()确实不是viewDidLoad()中的成员,而是嵌套函数。

将定义移动到类级别应该会有所帮助。但是,您还必须将drawView定义为成员(即在班级级别),以使其对clearDrawView()可见:

public class GameViewController : UIViewController {

    let drawView = CanvasView() // <--- move it here from viewDidLoad()

    public override func viewDidLoad() {
        super.viewDidLoad()

        // do the necessary stuff, including drawView setup
    }

    func clearDrawView() {
        drawView.clearCanvas()
    }

    @objc func clearCanvasButtonPressed() {
        clearDrawView() // note: not "GameViewController.clearDrawView()" because it's an instance method
    }
}

为了更好地理解Apple文档中的PropertiesMethods