gpt4 book ai didi

ios - Swift Playgrounds 类 Viewcontroller 没有成员 ""?

转载 作者:行者123 更新时间:2023-11-28 14:55:07 38 4
gpt4 key购买 nike

我是 swift 编程的新手,遇到了这个问题,老实说,我不知道如何解决这个问题。我在 Playground 上有一个 View Controller ,并且在该 View 上有一个按钮。我有一个名为 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() 的方法,用于清除 Canvas 。在我的 View Controller 中,我创建了一个继承自 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 上的 Playground 上而不是在 xcode 中制作的。一百万!

最佳答案

您当前的 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
}
}

为了更好地理解检查PropertiesMethods在 Apple 文档中。

关于ios - Swift Playgrounds 类 Viewcontroller 没有成员 ""?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465649/

38 4 0
文章推荐: html - Blackberry Web Browser (4.6.1) 和显示 block 元素
文章推荐: javascript - 从 jquery 设置输入类型键值
文章推荐: css - 当
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com