Home How to access variables when preparing for segue?
Reply: 0

How to access variables when preparing for segue?

user1528 Published in April 26, 2018, 1:46 pm

I'm preparing for a Segue (just learning at the moment!) and want to pass an array, which is created in a function, through a segue to arrive at the new view controller.

It's working fine if I just put a text string in there, but when I try change to an array it is blank - I think because it can't access the array because it's outside the scope - but I'm stuck on how to do it

Here's the initial VC code :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "goToWorkout" {
        if let destVC = segue.destination as? WorkoutViewController {
            destVC.workoutName = (testArray)

     //Get the new view controller using segue.destinationViewController.
     //Pass the selected object to the new view controller.

func testfunction() {
    let testArray = ["blah","blah","ploop"]

and the 'receiving VC code'

class WorkoutViewController: UIViewController {

var workoutName = [String]()

override func viewDidLoad() {


    // Do any additional setup after loading the view.

I'm nearly there with it but think I must be missing something basic. How do you do this passing arrays/other variables created in functions?

If the function had more than 1 variable/array, would that change the approach? I.e. one function might produce the exercises in the workout AND the number of reps for example

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.368361 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO