0
我試圖將參數傳遞給UIView子類。 我的代碼似乎工作,但初始化所有的參數成爲零後:當的lineWidth(參數之一)是所謂的繪製函數我有此錯誤:將參數傳遞給Swift 3中的UIView子類
"fatal error: unexpectedly found nil while unwrapping an Optional value"
這是我的代碼的viewController :
import UIKit
class ViewController: UIViewController,UIGestureRecognizerDelegate,UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var matrixView = UIMatrixView(mat: [[Bool]](),gRows:20,gCols:20,lWidth:2)
override func viewDidLoad() {
super.viewDidLoad()
// scrollView.delegate = self
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.matrixView
}
}
這是爲UIView子類:
import UIKit
class UIMatrixView: UIView {
var matrix : [[Bool]]!
var gridRows : Int!
var gridCols : Int!
var lineWidth : Int!
init(mat: [[Bool]], gRows : Int, gCols : Int, lWidth : Int){
print("here")
self.matrix=mat
self.gridRows=gRows
self.gridCols=gCols
self.lineWidth=lWidth
super.init(frame: CGRect())
print(lineWidth)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
print(lineWidth)
}
}
無論是通過出口或編程方式創建你的'matrixView'。你不能這樣做。 – rmaddy