Hi everyone,
I try to pass data from View Controller through Tab Bar Controller and Navigation Controller to Table View Controller. Screenshot of storyboard is bellow.
https://drive.google.com/open?id=1GW6gaDxfK1zaEOk2aPE8izkS5B67ldZL
My code is next:
1. Send data from View Controller
@IBAction func addToCarButton(_ sender: UIButton) {
tabBarController?.selectedIndex = 1
let navVC = tabBarController?.viewControllers![1] as! UINavigationController//
let cartTableViewController = navVC.topViewController as! CartTableViewController
cartTableViewController.titleItem = titleLabel.text
cartTableViewController.image = SomeImage(photo: imageView.image!)
}
2. Get data in Table View Controller
import UIKit
class CartTableViewController: UITableViewController {
// MARK: - Store data
// Create variables for receiving data (title and data from image) from VC
var titleItem: String?
var image: SomeImage?
// Create shopping cart - array of selected items
var cartItems = [Cart]()
var cartItem: Cart?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
//Load data from archive file
cartItems = Cart.loadFromFile()
// Create a new item
if titleItem != nil, image != nil {
print("titleItem in Cart View Appear - \(String(describing: titleItem))")
cartItem = Cart(title: titleItem!, image: image!)
}
// Add new item to shopping cart
if cartItem != nil {
cartItems.append(cartItem!)
}
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell", for: indexPath)
// Pass title to cell
cell.textLabel?.text = cartItems[indexPath.row].title
// Retrieve image from array of archive file and convert from Data to UIImage format
let image = UIImage(data: cartItems[indexPath.row].image.photo)
// Pass image to cell:
cell.imageView?.image = image
return cell
}
// MARK: - Save data to archieve file
override func viewWillDisappear(_ animated: Bool) {
Cart.saveToFile(cartItems: cartItems)
tableView.reloadData()
}
// Clear cart
@IBAction func clearCartButton(_ sender: UIBarButtonItem) {
cartItems.removeAll()
tableView.reloadData()
}
But data are appear only after second click. I.e., I add item 1 and cart is empty. When I add item 2, cart has item 1. If then I add item 3, card has item 1 and item 2, and go on.
I have done link bellow on gif that shows how it works.
https://drive.google.com/open?id=1_ilwKHXTRBT250zB4At7UsbWJ09fQARl
I tried to use different options, but I can not understand what is happening here.
Could you give me advice how to fix it?
I am not sure how it works, but could you try calling selectedIndex after:
@IBAction func addToCarButton(_ sender: UIButton) {
let navVC = tabBarController?.viewControllers![1] as! UINavigationController
let cartTableViewController = navVC.topViewController as! CartTableViewController
cartTableViewController.titleItem = titleLabel.text
cartTableViewController.image = SomeImage(photo: imageView.image!)
tabBarController?.selectedIndex = 1
}