在我的代码中,我调用一个函数将自定义UIButton的图像从蓝色按钮更改为不同颜色的按钮。
func changeButtonColor(_ colorChange: String, buttonToChange: UIButton) {
switch colorChange {
case "Blue": buttonToChange.setImage(#imageLiteral(resourceName: "blue-glossy-button-blank-hi"), for: .normal)
print("Blue")
case "Green": buttonToChange.setImage(#imageLiteral(resourceName: "asia2Greenadd"), for: .normal)
print("green")
case "Red": buttonToChange.setImage(#imageLiteral(resourceName: "asia2Redadd"), for: .normal)
case "Purple": buttonToChange.setImage(#imageLiteral(resourceName: "asia2add"), for: .normal)
default: print("Error")
}
print("hello")
greenButton1.setImage(#imageLiteral(resourceName: "asia2add"), for: .normal)
}
此功能位于希望修改的View Controller中。
奇怪的是,"Blue"
和"Green"
会在应有的时候打印出来。在打印"Hello"
之后,我尝试再次使用相应的插座设置按钮图像,但当它返回到我的视图控制器时,所有按钮都是与之前相同的图像。我必须在viewDidLoad
中进行此图像设置吗?我错过了修改视图的步骤吗?
这是我致电changeButtonColor
class EditorViewController: UIViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Back" {
if let destinationVC = segue.destination as? ViewController {
destinationVC.tagColorPairings[button!] = colorToSendBack
delegate?.changeButtonColor(colorToSendBack, buttonToChange: (button)!)
}
}
}
分辨
- 当我真的需要使用self.navigationController?.popToRootViewController