Thomas M. Thomas M. - 1 year ago 57
Swift Question

how to pass an array to another view controller in swift?

I have looked around for a good example of how to pass an array created and populated in 1 view conroller and pass it to another through a modal segue.

Here is my storyboard

see here

The user clicks the history button and it pops open the modal table view controller. The table view is then populated with the contents of an array send or passed from the original view.

When I print the contents of the array in the Destination View Controller its empty. So i think there is a problem with how its getting initialized.

Here is my code so far

This is the 1st UIView controller

//UIPickerView BarButtonItem done button actions here

func donePicker() {


var feet = Int(item1.componentsSeparatedByString(" ")[0])!
let inches = Int(item2.componentsSeparatedByString(" ")[0])!

feet *= 12

let totalInches = feet + inches



txtHeight.text = ""

let alertController = UIAlertController(title: "\(feet / 12) ft" + " " + "\(inches) in", message: "Entry Sucessfully Added", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))

self.presentViewController(alertController, animated: true, completion: nil)

//UIPickerView BarButtonItem cancel button actions here

func cancelPicker() {

txtHeight.text = ""


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if (segue.identifier == "showHistory") {

let dVC = (segue.destinationViewController as! HistoryTableViewController)

dVC.totalHeightValuesDest = totalHeightValues


Here is the Destination or 2nd tableviewcontroller

import UIKit

class HistoryTableViewController: UITableViewController {

@IBAction func btnExit(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)

var totalHeightValuesDest: [Int] = []

override func viewDidLoad() {



override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return totalHeightValuesDest.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

cell.textLabel?.text = "\(totalHeightValuesDest[indexPath.row])"

return cell

Answer Source
  1. In the destination (HistoryTableViewController) create an array property.
  2. In the first view controller in prepareForSeque assign the property to the array you currently have.
  3. Continue with segue

In step #2, after dVC.totalHeightValuesDest = totalHeightValues, add the following: dVC.myNewArrayProperty = myCurrentArrayOfHistoryObjects. Obviously, you'll substitute the property name you created in the HistoryViewController and the current myCurrentArrayOfHistoryObjects with the name of the local array variable you have.