-3

There is one two-dimension array in my viewcontrollerOne.

var estRent = [
            ("Weekly Rent:", a),
            ("Monthly Rent:", b),
            ("Yearly Rent:", c),
               ]

The first dimension is constants and the second one is variables. I declare it in an ButtonAction function. I want to give the variables value and pass the whole array to another .swift file when I click the button. I already know how to jump from the viewcontroller via button.

But how to send the array to the .swift in the same time?

GJZ
  • 2,482
  • 3
  • 21
  • 37
  • You mean you want to send that array to another swift file ( viewController? ) when you change the page? – Mohamad Sheikh Aug 10 '16 at 07:04
  • read about prepareForSegue – Lu_ Aug 10 '16 at 07:09
  • That is probably what you are looking for: http://stackoverflow.com/questions/7864371/how-to-pass-prepareforsegue-an-object – Yannick Aug 10 '16 at 07:10
  • I mean I want to send the array from one swift file to another swift file when I click a button. The button action will change the viewcontroller. I want to use the array in the second swift file. – Guodong Rong Aug 10 '16 at 13:19
  • When you pushing with the help of Segue, need to pass data to second class variables or use there Model class. Need to learn about model class pattern and Navigation pattern. – Anand Nimje Aug 11 '16 at 07:13

2 Answers2

0

Declare an Array variable on SecondViewController & pass the value on button action like this -

var secondvc = SecondViewController();
secondvc.yourArray = estRent;

That's all.

Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
0

The first view controller that contains the array:

class ViewController: UIViewController {

  var estRent = [ ("Weekly Rent:", 512), ("Monthly Rent:", 1024), ("Yearly Rent:", 12120)]

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewController = segue.destination as? SecondViewController {
      viewController.estRent = estRent
    }
  }

}

In second view controller that you are planning to send the array to:

class SecondViewController: UIViewController {

  var estRent = [(String, Int)]()

  override func viewDidLoad() {
    super.viewDidLoad()
    print(estRent)
  }

}
Abedalkareem Omreyh
  • 2,180
  • 1
  • 16
  • 20
  • Hi, In the "viewController.array = self.estRent", there is an error said "cannot assign value of type [(String,String)] to type [Any]" – Guodong Rong Aug 10 '16 at 12:55
  • I also tried to just send the array[String], but there is an error in navigationController.topViewController, and if I use segue.destinationViewController, there is an error in self.estRent – Guodong Rong Aug 10 '16 at 13:34