gpt4 book ai didi

Swift 委托(delegate)显示 nil

转载 作者:行者123 更新时间:2023-11-30 12:55:47 27 4
gpt4 key购买 nike

我创建了一个 .xib 并将该 xib 添加到另一个 View 。我想在该 .xib 文件中添加委托(delegate)方法。这是我的代码:

.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”方法中,我得到的委托(delegate)值为零。

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

最佳答案

您必须在已实现该协议(protocol)的其他类中拥有 CustomCheckBoxWithCross 的实例。在该文件中将 CustomCheckBoxWithCross 实例的委托(delegate)属性设置为 self。

关于Swift 委托(delegate)显示 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40416777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com