gpt4 book ai didi

ios - 由于某种原因,委托(delegate)为空

转载 作者:可可西里 更新时间:2023-11-01 00:38:47 34 4
gpt4 key购买 nike

我已经调试了一天并决定我不知道是什么导致了我的应用程序中的错误。如果有人能帮我解决这个问题,那就太好了

所以我从类名为ManualScreenNib 文件创建了一个自定义UIViewxibsetup() 基本上位于 UIView 扩展中,它只是从 Nib 文件加载。我想将点击按钮从我的 View 发送到 ViewController。我没有直接将此 View 添加到 ViewController,因为我需要删除此 ManualScreen View 并在 Segment Control 时添加另一个 View 移动到另一个选项。

class ManualScreen: UIView {
var mManualViewListener:ManualViewListener!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

}
override init(frame: CGRect) {
super.init(frame: CGRect.zero)
xibSetup()
}
@IBOutlet weak var counterLabel: UILabel!{
didSet {
print("labelView: \(String(describing: counterLabel))")
}
}
@IBAction func addButton(_ sender: UIButton) {
if(mManualViewListener != nil){ ->>>this is always nil for some reason
print("insdie the listener counting")
mManualViewListener.addCount()
}else{
print("listener is nil")
}
}
func addListener(manualViewListener:ManualViewListener){
print("adding listener")
mManualViewListener = manualViewListener

}
}

这个 UIViewViewcontroller 中初始化,这个 Viewcontroller 也实现了我的 delegate 协议(protocol)。当我在 Viewcontroller 中初始化我的 customView 时,我将此 Viewcontroller 添加为 delegate

var manualScreen = ManualScreen()
manualScreen.addListener(manualViewListener: self)

我的委托(delegate)协议(protocol)是

protocol ManualViewListener {
func addCount()
}

设置监听器后,我应该能够使用 manualViewListener.addcount() 从我的 View 向 ViewController 发送一些事件(此处为按钮触摸)。但它说我的 manualViewListener 总是 nil。

我只是在这里编写了一小部分代码,因为编写所有代码是不可行的。如果有人想查看整个应用程序,这里是指向我正在工作的东西的 GitHub 链接。 https://github.com/Rikenm/Auto-Counter-iOS

目前看起来不太漂亮。我现在只是在研究功能。

最后感谢您的帮助。

最佳答案

你的问题在这里

override init(frame: CGRect) {    
super.init(frame: CGRect.zero)

xibSetup() // this is the problem maker
}

你在它上面添加一个相同类的新 View ,并确保它的监听器对象与你在此处实例化的屏幕 View 为零

mManualScreen = ManualScreen()
mManualScreen.addListener(manualViewListener: self)

//

  extension UIView{

func xibSetup() {
let view = loadFromNib()
addSubview(view)
stretch(view: view)
}

// 2. Loads the view from the nib in the bundle
/// Method to init the view from a Nib.
///
/// - Returns: Optional UIView initialized from the Nib of the same class name.
func loadFromNib<T: UIView>() -> T {
let selfType = type(of: self)
let bundle = Bundle(for: selfType)
let nibName = String(describing: selfType)
let nib = UINib(nibName: nibName, bundle: bundle)

guard let view = nib.instantiate(withOwner: self, options: nil).first as? T else {
fatalError("Error loading nib with name \(nibName) ")
}

return view
}
}

相反,你需要

var mManualViewListener:ManualViewListener! 

static func loadFromNib() -> ManualScreen {

let view = Bundle.main.loadNibNamed("ManualScreen", owner: self, options: nil)?.first as! ManualScreen
return view
}

mManualScreen = ManualScreen.loadFromNib()

mManualScreen.addListener(manualViewListener: self)

关于ios - 由于某种原因,委托(delegate)为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52994114/

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