class ViewController1: UIViewController {
@IBOutlet weak var image1: UIImageView!
目前此图像在属性检查器中设置为“myfirstimage”
@IBAction func changeimage(_ sender: Any) {
image1.image = UIImage(named: "mysecondimage")
}
单击changeimage
按钮时,图像会发生变化。但是,只要我转到另一个视图控制器并返回,图像就会回到“myfirstimage”。尽管移入和移出其他视图,我如何将“mysecondimage”更改为永久性?
答案 0 :(得分:-1)
import UIKit
struct Globalvariable {
static var setimage = 0
}
class ViewController1: UIViewController {
@IBOutlet weak var image1: UIImageView!
inspector
override func viewDidLoad() {
super.viewDidLoad()
if Globalvariable.setimage == 0 {
image1.image = UIImage(named: "myfirstimage")
}
else {
image1.image = UIImage(named: "mysecondimage")
}
@IBAction func changeimage(_ sender: Any) {
image1.image = UIImage(named: "mysecondimage")
Globalvariable.setimage = 1
}
我能够通过在类外部的结构中创建变量并将图像添加到连接到Globalvariable的viewDidLoad中来解决这个问题。不确定这是否是最好的方法,但它是有效的。