Transfer data on return

I have 2 views ViewController A and ViewController B I have data in ViewController A in tableview

I display the data for each cell in the ViewController B in ViewController B i have TextFiled and TextView Are the same ViewController B I am modifying the data

But when I edit, I have a save button in which I save the new data After saving, I want to go back to ViewController A through the . button Back item

But the back button does not bring back the tableview functions again

I tried viewwillappear But it executes the function twice before and after

Is there a function that we only call on the back button?

It would be easier to read with some punctuation.

Is it this:

  • I have 2 views : ViewController A and ViewController B
  • I have data in ViewController A. Data is in tableview
  • I display the data for each cell in the ViewController B.

// How do you get there ? A segue ?

  • in ViewController B i have TextField and TextView.
  • Are the same ViewController B I am modifying the data. // I DO NOT UNDERSTAND THIS. What do you mean ?

.

  • But when I edit, // WHAT DO YOU EDIT ?

  • I have a save button in which I save the new data

  • After saving, I want to go back to ViewController A through the . button Back item // HOW DID YOU define this button ?

  • But the back button does not bring back the tableview functions again.

  • // WHERE DOES IT BRING BACK ?

  • // What is the code of the Back button ?

  • I tried viewwillappear // WHAT do you mean ? What did you put uin viewWillAppear ?

  • But it executes the function twice before and after

  • Is there a function that we only call on the back button?

Yes, there is, but we need first to understand what your code is.

Scroll through this menu in ViewController A

        
        let AddMenuItem = UIMenu(title: "", options: .displayInline, children: [
            
            UIAction(title: "new folder", image: UIImage(systemName: "folder.fill.badge.plus") ) { _ in
                self.alertNewFolder()
            },
            UIAction(title: "new image or video", image: UIImage(systemName: "camera.fill") ) { _ in
                    self.getMedia()
            },
            UIAction(title: "text", image: UIImage(systemName: "note.text.badge.plus") ) { _ in
                
//Here I move to ViewController B
                let vc = self.storyboard?.instantiateViewController(withIdentifier: "VCText") as! VCText
                vc.delgateText = self
                self.navigationController?.pushViewController(vc, animated: true)
            },
        ])
        return AddMenuItem
    }

This is ViewController B

class VCText: UIViewController {

    @IBOutlet weak var doneBtn: UIButton!
    @IBOutlet weak var nameNote: UITextField!
    @IBOutlet weak var detilesNote: UITextView!
    var delgateText : addTextMenu?
    
    var arrS1 : [addCatogrey] = []
    var name : String?
    var detl : String?
    override func viewDidLoad() {
        super.viewDidLoad()

        nameNote.text = name
        detilesNote.text = detl
    }
    
    @IBAction func doneBtnClicke(_ sender: Any) {

        
        guard let name = nameNote.text , !name.isEmpty else {
            return
        }
        guard let detiles = detilesNote.text else {
            return
        }
        arrS1.append(addCatogrey(nameCatog: name, imageSection: UIImage(systemName: "note.text"), detilesNote: detiles, nameData: "note"))
        delgateText?.DelegateText(arrData: addCatogrey(nameCatog: name, imageSection: UIImage(systemName: "note.text"), detilesNote: detiles, nameData: "note"))

        doneBtn.isEnabled = false
        doneBtn.setTitle("", for: .normal)
    }
    

}

protocol addTextMenu {
    func DelegateText (arrData : addCatogrey)
}

I am making the edit from the ViewController B I want to press the back button and find the value has been modified

Read here swift guide on closures.

class VCText: UIViewController {
    ...
    var completionHandler: (([addCatogrey])->Void)?
    ...
    @IBAction func doneBtnClicke(_ sender: Any) {
        ...
        /// return a copy of the array no need to create the object already added to the array a second time as done in your code using the delegate protocol pattern
        completionHandler?(arrS1)
    }
}

In view controller A

      ...
      let vc = self.storyboard?.instantiateViewController(withIdentifier: "VCText") as! VCText
      vc.delgateText = self // get rid of this
      /// define body of completion handler here
      vc.completionHandler = { array in 
           /// do something with the array in view controller A
      }
      ...
Transfer data on return
 
 
Q