Sending Array Data from One View Controller to Another

Pass array to another View Controller

The reason why it doesn't work is that you instantiate a new HomeTableViewController with empty data.

If this data will be used on lots of place, why not save it locally? user default seems like it fit your needs.

func sendDataToCalendar() {
UserDefaults.standard.set(workoutsName, forKey: "workoutsName")
}

Then you can read it later on

func getWorkoutNameArray() {
let workoutName = UserDefaults.standard.object(forKey: "workoutsName") as? [String]

}

Passing array to another VC (Swift)

If you're using storyboards, typically you'll use a segue to link from one view controller to another. In that case, prepareForSegue is the usual place to pass info from one view controller to another.

You need to create a property in the destination view controller to receive the array.

If you add a property array of custom type YourArrayType, to ViewControllerHistory, the first part of that class might look like this:

class ViewControllerHistory: UIViewController {
var array: YourArrayType
}

And then the code in the first view controller might look like this:

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? ViewControllerHistory else
{
return
}
destination.array = arrayWithResults
}

A couple of notes:

Don't use arrays of AnyObject type. Swift supports arrays containing specific types of objects. You should prefer arrays of a specific type over arrays of AnyObject

Variable names should start with a lower case letter. Class names and types should start with an upper-case letter. Therefore your ArrayWithResults should be arrayWithResults instead.

If you're invoking your ArrayWithResults view controller directly rather than with a segue, you can simply create the view controller using instantiateViewController(withIdentifier:), set the property, then present or push it.

How to pass an array from one view controller to another?

you can do it by defining an array property in second viewcontrollers .h file like:

@interface SecondViewController : UIViewController 
@property(nonatomic, strong)NSArray *array;
@end

Now in FirstViewconrtoller just pass it

SecondViewController *controller = [[SecondViewController alloc]....]
controller.array = yourArray.//the array you want to pass

Passing a Struct Array through a TableViewCell to another ViewController

If you want to share specific cell's data into table of other view controller, then you need to create an object of Model struct rather than its array just like below:

var newFeed: Model?

Next you can assign it value of particular cell before navigation in main did select method as below:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let vc = storyboard?.instantiateViewController(withIdentifier: "Comment") as? Comment else {return}
vc.newFeed = getInfo[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}

It will assign that cell's data to newFeed Variable and relaod table with that data.

For any question, feel free to ask.

How to pass data in array from one vc to another?

Update: Seems you're not registering your UITableViewCell here, add this line to viewDidLoad if you've created the cell programmatically (if you're using nib then register cell using nib parameter):

tableView.register(FilterCell.self, forCellReuseIdentifier: "CategoryCell")

Add an observer for arrayOfcategories for when it sets to reload your data to the UITableView like this:

var arrayOfcategories: [String]? {
didSet {
tableView.reloadData()
}
}

Pass Array from View Controller To View Controller Returns Empty Array

This works. You can access it directly. I'm not sure the exact reasoning, so someone else is welcome to explain.

In VC 2 - declare variable.

/// A subclass of `UIViewController` with a `MessagesCollectionView` object
/// that is used to display conversation interfaces.
open class MessagesViewController: UIViewController,
UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

public static var arrayNameInVCTwo : NSMutableArray? = nil

In VC 1 - reference variable from VC 2

let arrayNameInVCOne = MessagesViewController.arrayNameInVCTwo

how can I pass a swift array by reference between viewControllers in prepare(for segue:?

Create wrapper class for your array and then pass that class reference wherever you want:

class ArrayWrapper {
let array: [YourType]

init(array: [YourType]) {
self.array = array
}
}

Swift. Passing persistent data from one View Controller to another and storing within a label array

You are right on your comment,

which the Dictionary is overriding as I use the same key

Each time you tap that button, you are overriding the value with the new one. So you should be seeing always on your v2, the last one.

So probably you should store an array instead of a String.

    //on v1
var values = [String]()

@IBAction func saving(_ sender: UIButton) {
let savedText = "Gallon \(gallonTextFieldOutlet.text) is equal to Litre \(litreTextFieldOutlet.text) is equal to Pint \(pintTextFieldOutlet.text)"
values.append(savedText)
UserDefaults.standard.setValue(values, forKey: "SavedConversion")
}

You will pass it to v2 as you are doing now

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "savedVolumeData"
{
let historyVC:VolumeHistoryViewController = segue.destination as! VolumeHistoryViewController

if let values = userDefault.value(forKey: "SavedConversion") as? [String] {
historyVC.volumeDataOne = values //volumeDataOne now needs to be an [String]
}
}

Then on V2, go through your array and labels.



Related Topics



Leave a reply



Submit