gpt4 book ai didi

ios - 当我想在另一个 VC 中更改 VC 中的某些内容时,是否必须使用委托(delegate)? swift

转载 作者:行者123 更新时间:2023-11-30 12:20:09 25 4
gpt4 key购买 nike

我现在正在做一些练习。例如,我在 Storybard 中创建两个 View Controller ,并且我想通过单击按钮来更改颜色,例如从 VC2 到 VC1。在这种情况下需要委托(delegate)还是有其他方法可以做到这一点?

最佳答案

通常情况下,是的。但这要看情况。您可以使用NotificationCenter,您可以使用“准备”功能。委托(delegate)是这里最常见的选项。对于您的情况:

protocol ChangeButtonColorDelegate {
func changeButtonColor()
}

class VC1 : ChangeButtonColorDelegate... {
func changeButtonColor() {
/* change button color here */
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "goto_vc2") {
(segue.dest as! VC2).delegate = self
}
}
}

class VC2 : ... {
var delegate : ChangeButtonColorDelegate!
}

关于ios - 当我想在另一个 VC 中更改 VC 中的某些内容时,是否必须使用委托(delegate)? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44892667/

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