快速代表显示无

时间:2016-11-04 06:44:05

标签: swift delegates

我创建了一个.xib并将此xib添加到另一个视图中。我想在.xib文件中添加委托方法。这是我的代码:

.xib文件

import UIKit

protocol CustomCheckBoxWithCrossDelegate {

    func deleteCustomCheckbox(itemToDelete: String)
}

class CustomCheckBoxWithCross: UIView {

    @IBOutlet weak var radioButton: UIButton!
    @IBOutlet weak var checkboxLbl: UILabel!
    @IBOutlet weak var checkboxCrossBtn: UIButton!

    var blockIndexForTheCrossButton: Int = 0
    var delegate: CustomCheckBoxWithCrossDelegate!

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

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


    func loadViewFromNib() {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: "CustomCheckBoxWithCross", bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        view.frame = bounds
        view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        self.addSubview(view);
    }


    @IBAction func crossBtnTapped(sender: AnyObject) {
        print("Delegate --> \(delegate)")
        delegate!.deleteCustomCheckbox("hello")
    }
}
  • 但是在“crossBtnTapped”方法中,我获得该代表的零值。

谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

您必须在其他类中具有CustomCheckBoxWithCross实例,您已在其中实现了协议。将CustomCheckBoxWithCross实例的委托属性设置为该文件中的self。

答案 1 :(得分:0)

之前我曾多次遇到此错误,如果委托不是零条件,请添加。

@IBAction func crossBtnTapped(sender: AnyObject) {

    if delegate != {
    delegate!.deleteCustomCheckbox("hello")
    }
}