gpt4 book ai didi

ios - Xcode( swift ): Changing UIButton attributes in a different UIViewController class than that in which they were referenced

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:10 26 4
gpt4 key购买 nike

在我提出实际问题之前的快速说明:我对 XCode 和 Swift 还很陌生(刚接触几天)。我在 matlab、mathematica 和 IGOR 等科学平台上有编程经验。

我的问题:在我的主视图 Controller 中,我有一个用于 UIButton 的 IBOutlet,我已经在主 Storyboard中实现了它。

class ViewController: UIViewController {

....

@IBOutlet weak var chip_a1: UIButton!

....
}

同样在我的主视图 Controller 中,在 IBAction 方法中,我能够按如下方式访问隐藏属性:

@IBAction func chip_a1_touchupinside(sender: UIButton) {
self.chip_a1.hidden = true
}

但是当我尝试从不同的 View Controller 类中访问此属性时,如下所示:

class endgame: UIViewController {  
....
@IBAction func playagain_touchupinside(sender: UIButton) {
var handle = ViewController()
// make the states of all the chips visible
handle.chip_a1.hidden = false // this is the line that gives me an ERROR
dismissViewControllerAnimated(true, completion: nil)
}
....
}

错误是这样的:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

如果您能帮助理解/解决此问题,我们将不胜感激。如果需要,我很乐意提供更多详细信息。

最好的,亚历克斯。

最佳答案

你用 xib 文件创建 UIViewcontroller

你必须改变

var handle = ViewController()

var handle = ViewController(nibName: "ViewControllerNibfileName", bundle: nil)

因为 Button 是 IBOutlet 并且需要用 viewcontroller 加载的 nibfile

关于ios - Xcode( swift ): Changing UIButton attributes in a different UIViewController class than that in which they were referenced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27635162/

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