I have this button whenever I click this button, I want to append new data to a struct on another viewcontroller and show it on tableview and when I go back to add more data the prev data wont gone. In my case, I can only add 1 data, after I go back and add more data, the previous one is gone.
ViewController segue code and storyboard:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "main1btn") {
let vc = segue.destination as! ConfirmViewController
vc.data.append(ConfirmViewController.menu(menuImages: "main1",menuPrice: 120000))
}
if (segue.identifier == "main2btn") {
let vc = segue.destination as! ConfirmViewController
vc.data.append(ConfirmViewController.menu(menuImages: "main2",menuPrice: 150000))
}
if (segue.identifier == "main3btn") {
let vc = segue.destination as! ConfirmViewController
vc.data.append(ConfirmViewController.menu(menuImages: "main3",menuPrice: 120000))
}
}
viewcontroller
confirmviewcontroller code and storyboard:
class ConfirmViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
struct menu {
let menuImages : String
let menuPrice : Int
}
@IBOutlet weak var totalLbl: UILabel!
@IBOutlet weak var confirmBtn: UIButton!
@IBOutlet weak var cartTable: UITableView!
var data: [menu] = [
]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CartTableViewCell
let Menu = data[indexPath.row]
cell.harga.text = String (Menu.menuPrice)
cell.foto.image = UIImage(named: Menu.menuImages)
return cell
}
confirmcontroller storyboard
CodePudding user response:
when you go back to previous controller your VC is removed from navigation controller so the object of ConfirmViewController removed from memory so when you again pushed ConfirmViewController in navigation stack that new object for that class again initialised and at that time array of data is Empty and before pushing you again add one item in data which you are adding while moving your segue. This is why your previous items are removing .SUGGESTED SOLUTION: If you want to hold all items data than you can define another class for Holding Data and that will not be clear from memory and when your ConfirmViewController appears get all list from that class and populate that data
class DataRack : NSObject{
static let sharedManager : DataRack = DataRack()
var itemsData : [menu] = []
}
One more thing when you are done with these items you have to clear it from that DataRack Class
CodePudding user response:
You have option for this
if you want to store data permanently then store into UserDefaults like this.
First screen :---
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "main1btn") {
let vc = segue.destination as! ConfirmViewController
}
if (segue.identifier == "main2btn") {
let vc = segue.destination as! ConfirmViewController
}
if (segue.identifier == "main3btn") {
let vc = segue.destination as! ConfirmViewController
}
var newData = [menu]()
if let data = UserDefaults.standard.object(forKey: "yourKey") as? [menu] {
newData.append(data)
} else {
newData.append(menu(menuImages: "main3",menuPrice: 120000))
}
UserDefaults.standard.setValue(value: newData, forKey: "yourKey")
}
Second Screen :--
class ConfirmViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var data = [menu]()
override func viewDidLoad() {
super.viewDidLoad()
if let data = UserDefaults.standard.object(forKey: "yourKey") as? [menu]{
data = data
}
}
}
OR
if you do not want to store data then take
struct menu {
let menuImages : String
let menuPrice : Int
}
and
var data: [menu] = [
]
variable globally removeAll while you not need data