2017-01-07 7 views
0

UIView 하위 클래스에 인수를 전달하려고합니다. 이것은의 ViewController에 대한 내 코드입니다Swift 3의 UIView 하위 클래스에 인수 전달

"fatal error: unexpectedly found nil while unwrapping an Optional value"

: 선폭 (인수 중 하나) 그리기 함수를 호출 할 때 나는이 오류를했습니다 : 내 코드가 작동하지만, 초기화 모든 인수가 전무 후 보인다 :

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) 
    } 
} 
+2

어느 콘센트 나, 프로그램을 통해 matrixView' '를 생성한다. 둘 다 할 수는 없습니다. – rmaddy

답변

2

약한 속성으로 표시된 지정하여 UIView.

강력한 참조 코드 작성 후 유지되지 않습니다.

IBOutlet으로 표시된 속성은 스토리 보드에서 설정할 수 있습니다.

하거나, 설정된 약한 참조 하위 뷰 (addSubview)로 투입 후 :

func appendMatrixView() { 
    let mView = MatrixView(... 
//Important lines..... 
    addSubview(mView) 
    self.matrixView = mView; 
}