ios - 关闭后对前一个视图控制器的操作
问题描述
我有两个视图控制器。通过按 button1,我从 VS_1 更改为 VS_2。通过按下按钮 2,我转到视图控制器 1。此时我需要 button1 将其颜色更改为红色。怎么做?
class VC_1: UIViewController{
..........
var button1 = UIButton()
button1.backgroundColor = .green
@IBAction func goToVC_2(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vC2 = storyBoard.instantiateViewController(withIdentifier: "VC2") as! VC_2
self.present(vC2, animated: true, completion: nil)
}
}
class VC_2: UIViewController{
..........
var button2 = UIButton()
button2.backgroundColor = .green
@IBAction func goToVC_1(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
}
解决方案
您可以通过回调、委托等方式完成。
通过回调:
@IBAction func goToVC_2(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vC2 = storyBoard.instantiateViewController(withIdentifier: "VC2") as! VC_2
vC2.didTapButton = {
// change color here
}
self.present(vC2, animated: true, completion: nil)
}
var didTapButton: () -> Void = { }
@IBAction func goToVC_1(_ sender: Any) {
didTapButton()
self.dismiss(animated: true, completion: nil)
}
推荐阅读
- visual-studio-code - VSCode 在所选内容或单个 Word 周围插入双圆括号
- migration - 将消费逻辑应用迁移到标准
- javascript - 使用“文本框”创建问卷 Javascript
- python - 通过 lambda 测试测试 Rotating Secret
- python-3.x - 您如何将 1 周的 yfinance 间隔设置为默认为星期五而不是星期一?
- swift - Firebase 登录后不显示用户
- python - IDA Pro:如何获取导入函数?(或获取导入表列表的 API)
- javascript - 如何使用 VUE 为课程添加条件
- xcode - Can you change info.plist inside a Run Script in Xcode's Build Phase?
- c - 分配基地址的指针数组给出错误