Edward Lim Edward Lim - 1 year ago 70
iOS Question

Passing data from a table view controller to a another view controller contained within tab view?

So, As of right now what I understand about Swift is that in order to transition from one view to another the way to go about it is using segues. However, in my app I have a table view which shows a list of items where the user can interact with it and doing so will move the items to another view controller in my app, contained within a tab bar.

class JobCompletedTableViewController: UITableViewController {

// MARK: Properties
var jobsCompleted = [Job]()
var jobsChosen = [Job]()

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
// Add data to the array created locally

share.backgroundColor = UIColor.blue

return [share]
// Need some sort of method to pass data into other view
//controller if tab button is clicked

The Table View that is embeded within a navigation controller and Tab Bar view controller.

class ChosenTableViewController: UITableViewController {

// MARK: Properties
var jobsChosen = [Job]()

// Upon initalization of this view, this array should be populated with data passed from the above view controller

My initial thought on this problem was to simply create an array in the view controller that tracks user interaction with list items. Then add items that user interacted with to array. When user clicks on item in tabbar, instantiate the other table view controller which shows all items that user interacted with.

I would appreciate it if you would go into how I can actually instantiate an instance of the ChosenTableViewController class which will hold all the list of items user interacted with.

Storyboard, to give an idea of what My app looks like and how its contained
enter image description here

Answer Source

Since they don't need to be persisted, the simplest solution would be to use a singleton that will hold the selected Jobs in an array that you can retrieve from the e-mail VC when it appears.

final class SelectedJobs {

static let shared = SelectedJobs()

var jobsArray = [Job]()

Then in the e-mail VC:

 class EmailVC: UIViewController {
    var selectedJobs = [Job]()

    override func viewWillApear(_ animated: Bool) {

         for job in SelectedJobs.shared.jobsArray {


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download