2017-12-16 31 views
0

CustomViewのIBOutletsはゼロになります。Exc_Bad_Access XibからCustomViewをロード中に

カスタムビュー(xib)を作成しました。

詳細は画像をご覧ください。

class TextFieldView: UIView { 

@IBOutlet var contentView: TextFieldView! 
@IBOutlet weak var customTextField: UITextField! 
@IBOutlet weak var rightButton: UIButton! 
@IBOutlet weak var placeHolderLabel: UILabel! 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    commonInit() 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    commonInit() 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 

func commonInit() 
{ 
let bundle = Bundle(for: type(of: self)) 
bundle.loadNibNamed("TextFieldView", owner: self, options: nil) 
customTextField.backgroundColor = UIColor.green 
NSLog("Called") 
} 

はまだEXC_BAD_ACCESSでエラーを投げ

+1

何の画像を?これらのアウトレットをXIBに接続しましたか? –

答えて

0

(コードのloadNibNamed行で)あなたが何か必要があります。

/// This view must be connected with the main view of the XIB. 
@IBOutlet private var view: UIView! 

/// This method is used when creating an `ApexView` with code. 
override init(frame: CGRect) 
{ 
    super.init(frame: frame) 

    initView() 
} 

/// This method is called when instantiated from a XIB. 
required init?(coder aDecoder: NSCoder) 
{ 
    super.init(coder: aDecoder) 

    initView() 
} 

private func initView() 
{ 
    /// Fill in the name of your XIB. I assumed it `"TextFieldView"`. 
    Bundle.main.loadNibNamed("TextFieldView", owner: self, options: nil) 

    self.view.frame = CGRect(x: 0.0, y: 0.0, width: self.width, height: self.height) 
    self.addSubview(self.view!) 

    ... 
} 
+1

'Bundle.main'は使わないでください。 'Bundle(for:class)'を使ってください。 – Sulthan

+0

@Sulthanどうして、 'Bundle(for:class) 'の使い方が見えますか? –

+1

'Bundle(for:type(of:self))')を使用すると、IBからロードされたdesignablesで作業を開始し、メインバンドルが異なることがわかります。 – Sulthan

関連する問題