@@ IBOutlet在选择器方法中为零

问题描述 投票:0回答:2

我在代码中发现了一个非常奇怪的问题。我以编程方式制作了一个UIButton,并使用#selector和@objc方法添加了一些目标。但是,在此@objc方法中,我无法访问另一个@IBOutlet,我始终会遇到此致命错误:意外地找到nil,同时隐式展开了一个可选值:file。

这是我的按钮,连接良好

@IBOutlet weak var btn_selectAllGamme: UIButton!

按钮的代码

let btn: UIButton = UIButton(frame: CGRect(x: Int(destinationController.view_gammePE.bounds.width/2+12), y: Int(destinationController.view_saison.bounds.height/2-30)+(k*55), width: 184, height: 40))
    btn.setTitle(val, for: .normal)
    btn.setTitleColor(UIColor.black, for: .normal)
    btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
    btn.addTarget(self, action: #selector(choixSaison_click(_:)), for: .touchUpInside)
    btn.addTarget(self, action: #selector(touchDown_click(_:)), for: .touchDown)
    btn.addTarget(self, action: #selector(dragExit_click(_:)), for: .touchDragExit)

和选择器:

@objc func touchDownGamme_click(_ sender : UIButton){
        self.btn_selectAllGamme.setTitle("Test", for: .normal)
}
swift selector iboutlet
2个回答
0
投票

定义您要使用的变量

@IBOutlet weak var btn_selectAllGamme: UIButton!

然后创建它-但是使用您定义的变量,而不是新变量

btn_selectAllGamme = UIButton(frame: CGRect(x: Int(destinationController.view_gammePE.bounds.width/2+12), y: Int(destinationController.view_saison.bounds.height/2-30)+(k*55), width: 184, height: 40))
btn.setTitle(val, for: .normal)
btn.setTitleColor(UIColor.black, for: .normal)
btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .semibold)
btn.addTarget(self, action: #selector(choixSaison_click(_:)), for: .touchUpInside)
btn.addTarget(self, action: #selector(touchDown_click(_:)), for: .touchDown)
btn.addTarget(self, action: #selector(dragExit_click(_:)), for: .touchDragExit)

和选择器:

@objc func touchDownGamme_click(_ sender : UIButton){
    sender.setTitle("Test", for: .normal)
}

0
投票

我认为您不理解我的问题。我只想在这两个按钮之间进行交互,当我点击btn时,我想更改btn_selectAllGamme的标题!

© www.soinside.com 2019 - 2024. All rights reserved.