0

I am trying to build upon answer which I was given here. What I am trying to is very simple - I want a text field which you can enter text into. You press the go button and it takes you to a new view and replaces the text on a label on that page with whatever the user entered in the box. The is the code I am using on the first page.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var entry: UITextField!

    let dictionary = entry.text // Line 7 ERROR


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "viewTwo"
        {
            if let destinationVC = segue.destinationViewController as? viewTwo{
                destinationVC.dictionary = self.dictionary // Line 24 ERROR
            }
        }
    }

    @IBAction func goToViewTwo(sender: AnyObject) {
        performSegueWithIdentifier("viewTwo", sender: self)
    }

}

I am only including the code from the first view because i know the code from the second view is working.

I didn't encounter an error until I tried to use the text field - before when I just had a pre-choses text to transfer over it worked. Before, instead of having let dictionary = entry.text I had let dictionary = "foo" and it worked.

So my question is exactly the same thing but have a text field instead of pre-chosen text - what I really want to know is why my code didn't work before.

The errors I got were on line 7 (I have labeled the lines above which had the errors) - 'ViewController.Type' does not have member names 'entry' and there was also an error on line 24 but I suspect this is related to this error and will be fixed if this error is also fixed. Just incase though, the error on line 24 was: 'ViewController.Type' does not have member names 'dictionary'

Thank you.

Community
  • 1
  • 1
cross
  • 363
  • 1
  • 4
  • 15

2 Answers2

1

You should set the dictionary to var dictionary = "" in the declaration. You use var instead of let here, so that you can change the value of the dictionary later.

Then inside your @IBAction func goToViewTwo(sender: AnyObject){} method, you set the self.dictionary = entry.text

 @IBAction func goToViewTwo(sender: AnyObject) {
        dictionary = entry.text
        performSegueWithIdentifier("viewTwo", sender: self)
    }

Alternatively, you can just do the following inside prepareForSegue() method. This way, you dont need to declare a dictionary to hold the text value of your UITextField, you can just pass the text value from your entry to the second view controller's dictionary variable.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "viewTwo"
        {
            if let destinationVC = segue.destinationViewController as? viewTwo{
                destinationVC.dictionary = self.entry.text
            }
        }
    }
ztan
  • 6,861
  • 2
  • 24
  • 44
0

A dictionary is not constant, so declare it as lazy var, not let:

lazy var dictionary: String {
     return entry.text
}()
Undo
  • 25,519
  • 37
  • 106
  • 129
Neil Horton
  • 707
  • 6
  • 13