如何在Swift 3中保存具有用户默认值的自定义按钮的图像?

时间:2022-12-23 21:24:23

What I want to do is that the app starts with a custom button that has a simple image as a background. If the user taps on the "enter" button, the image of the custom button will permanently change, and if the user restarts the app, the second image will still appear instead of the first one. 如何在Swift 3中保存具有用户默认值的自定义按钮的图像? 如何在Swift 3中保存具有用户默认值的自定义按钮的图像?

我想要做的是应用程序以自定义按钮开始,该按钮具有简单的图像作为背景。如果用户点击“输入”按钮,则自定义按钮的图像将永久更改,如果用户重新启动应用程序,则第二个图像仍将显示而不是第一个图像。

2 个解决方案

#1


1  

When the button clicked, save a flag value in UserDefaults:

单击该按钮时,在UserDefaults中保存标志值:

UserDefaults.standard.set("1", forKey: "kIsButtonSelected")
UserDefaults.standard.synchronize()

When relaunch the app, check the value and set the image for button:

重新启动应用程序时,请检查值并将图像设置为按钮:

if let isButtonSelected = UserDefaults.standard.object(forKey: "kIsButtonSelected") as? String {
    if isButtonSelected == "1" {
        //set the second image
    }
}

And a better practice is to set first image for button's normal status, and second one for selected status. And just set the button's status when the flag value is detected:

更好的做法是为按钮的正常状态设置第一个图像,为选择状态设置第二个图像。只需在检测到标志值时设置按钮的状态:

button.isSelected = true  //the image will be changed to the second one automatically.

#2


0  

I would do this, so that way you can use more than 1 picture if desired:

我会这样做,所以如果需要你可以使用超过1张图片:

// Put a all of your images that you want here:
var imageDictionary = ["smilyface": UIImage(named: "smilyface.png"),
                       "person"   : UIImage(named: "person.png"),
                       // and so on...
]

// Use this whenever you want to change the image of your button:
func setCorrectImageForButton(imageName name: String) {
  UserDefaults.standard.set(name, forKey: "BUTTONIMAGE")
}

// Then use this to load the image to your button:
// myButtonImage = grabCorrectImageForButton() 
func grabCorrectImageForButton() -> UIImage? {

  guard let imageKey = UserDefaults.standard.string(forKey: "BUTTONIMAGE") else {
    print("key not found!")
    return nil
  }

  if let foundImage = imageDictionary[imageKey] {
    return foundImage
  }
  else {
    print("image not found!")
    return nil
  }
}

#1


1  

When the button clicked, save a flag value in UserDefaults:

单击该按钮时,在UserDefaults中保存标志值:

UserDefaults.standard.set("1", forKey: "kIsButtonSelected")
UserDefaults.standard.synchronize()

When relaunch the app, check the value and set the image for button:

重新启动应用程序时,请检查值并将图像设置为按钮:

if let isButtonSelected = UserDefaults.standard.object(forKey: "kIsButtonSelected") as? String {
    if isButtonSelected == "1" {
        //set the second image
    }
}

And a better practice is to set first image for button's normal status, and second one for selected status. And just set the button's status when the flag value is detected:

更好的做法是为按钮的正常状态设置第一个图像,为选择状态设置第二个图像。只需在检测到标志值时设置按钮的状态:

button.isSelected = true  //the image will be changed to the second one automatically.

#2


0  

I would do this, so that way you can use more than 1 picture if desired:

我会这样做,所以如果需要你可以使用超过1张图片:

// Put a all of your images that you want here:
var imageDictionary = ["smilyface": UIImage(named: "smilyface.png"),
                       "person"   : UIImage(named: "person.png"),
                       // and so on...
]

// Use this whenever you want to change the image of your button:
func setCorrectImageForButton(imageName name: String) {
  UserDefaults.standard.set(name, forKey: "BUTTONIMAGE")
}

// Then use this to load the image to your button:
// myButtonImage = grabCorrectImageForButton() 
func grabCorrectImageForButton() -> UIImage? {

  guard let imageKey = UserDefaults.standard.string(forKey: "BUTTONIMAGE") else {
    print("key not found!")
    return nil
  }

  if let foundImage = imageDictionary[imageKey] {
    return foundImage
  }
  else {
    print("image not found!")
    return nil
  }
}