0

I have ViewControler.swift (main) and ViewControllerHistory.swift (for history).

How can I make the history stored in VC (for history) after pressing the button (=).

I have code to store the values into an array.

How can I send this array to the ViewControllerHistory.swift (for history)

@IBAction func EqualSave(_ sender: UIButton) {
    let result = "\(firstOperand)\(equalitySignPressed)\(secondOperand)="
    ArrayWithResults.append(result as AnyObject)
}

VC (for history)

Screen Main VC

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • There are a couple of ways of doing this depending on if you are using Storyboards segues or programmatically moving to your ViewControllerHistory.swift. Need more into to help. – Chi-Hwa Michael Ting Oct 08 '17 at 13:12
  • @Chi-HwaMichaelTing I using Storyboard –  Oct 08 '17 at 13:19

1 Answers1

2

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.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • var array = ArrayWithResults (Error - Use of unresolved identifier 'ArrayWithResults') –  Oct 08 '17 at 13:37
  • My answer is not intended to be copy/pasted into your code. You'll have to edit it to fit into your program. – Duncan C Oct 08 '17 at 15:57