Problem recovering view saved in file

0

I have done the following example in which I create a button, I add a function for the touch up event and save it in file. The problem is that when you recover the button from the file, it no longer responds to the touch up. I could add it after loading it, but the views saved in file can be several and of different types, and it would be a bit cumbersome to add the events according to the type of view. I have tried creating the button from the storyboard instead of programmatically but the result is the same. To see if someone indicates to me that I am leaving or if this is so:

import UIKit
class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let b = UIButton(frame: CGRect(x: 50, y: 50, width: 50, height: 50))
    b.backgroundColor = UIColor.blue;b.tag = -100
    b.addTarget(self, action: #selector(ViewController.buttonClick(_:)), for: .touchUpInside)
    view.addSubview(b)
  }
  @IBAction func load(_ sender: UIButton) {
    let path = dataFilePath("file")
    if FileManager.default.fileExists(atPath: path) {
      if let data = NSData(contentsOfFile: path) {
        let unarchiver = NSKeyedUnarchiver(forReadingWith: data as Data)
        if let button = unarchiver.decodeObject(forKey: "button") as? UIButton {
          print("button cargado")
          unarchiver.finishDecoding()
          view.addSubview(button)
        }
      }
    }
  }
  @IBAction func save(_ sender: UIButton) {
    let button = view.viewWithTag(-100)
    let data = NSMutableData()
    let archiver = NSKeyedArchiver(forWritingWith: data)
    archiver.encode(button, forKey: "button")
    archiver.finishEncoding()
    data.write(toFile: dataFilePath("file"), atomically: true)
    button?.removeFromSuperview()
  }
  func buttonClick(_ sender: UIButton){
    print("Tap")
  }
  func documentsDirectory() -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    return paths[0]
  }
  func dataFilePath(_ name: String) -> String {
    return (documentsDirectory() as NSString).appendingPathComponent(name) as String
  }
 }
    
asked by jose 12.10.2016 в 18:30
source

1 answer

0
  

I could add it after loading it, but the views saved in file can be several and of different types, and it would be a bit cumbersome to add the events according to the type of view.

That's exactly what you have to do, because the actions are not stored when you serialize the buttons.

If you have too many buttons, what you should do is assign a value to the variable tag of each button before saving it to the file.

button.tag = 0 // 1,2,3,4...n

Then when you load each button from the file, you assign the same method using

button.addTarget(self, action: #selector(ViewController.buttonClick(_:)), for: .touchUpInside)

Finally in buttonClick you execute the action depending on the tag of the button

func buttonClick(_ sender: UIButton){
     print("Tap")

    if sender.tag == 0
    {
         //Ejecutar acción
    }
}
    
answered by 13.10.2016 / 04:37
source