0

Since i'm new and a newbie on Swift can someone tell me how I can get the label value on screen 1 to show on screen 2. Thanks

enter image description here

Edit: So I tried the way you told me but for some reason the Label text did not change in View 2. Any help? Thanks

enter image description here

enter image description here

Luke_i
  • 163
  • 1
  • 2
  • 13
  • Possible duplicate of [How do you share data between view controllers and other objects in Swift?](http://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift) – pableiros Nov 15 '16 at 15:22
  • Still you haven't assigned value in VIEW2. In vc2 inside your viewDidLoad try. Number1V2.text = labelNumber1 – Joe Nov 15 '16 at 19:34

3 Answers3

1

I'll consider you have ViewController class VC1 (screen 1) & ViewController class VC2 (screen 2). I see from the screenshot that, you're already using a segue to go from the VC1 to VC2 view.

Now, declare a variable in your VC2 class let's call it labelValue,

class VC2 {

  var labelValue: String?

...

}

Whenever you use a storyboard segue to move from one viewcontroller's view to other viewcontroller's view a method named,

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

will get called if it's overriden in the source viewController class (scene1 in your case). In this method you'd be able to access the destination view controller (scene 2) and it's properties. So for your case you can implement it like this,

class VC1 {

//I have assumed the below label will hold the value which you want to pass it to the scene 2.
var lblResult: UILabel!
......
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

// You can access the VC2 instance from the segue
  if let vc2 = segue.destinationViewController as? VC2 {

    vc2.labelValue = lblResult.text

  }

}

Now once you implement the prepareForSegue: method correctly as shown above, you'd be able to get the label's value from scene 1 to labelValue property of scene2.

Do learn more about segues here : More About Segues and Storyboards here.

================================

Edit:

Coming to your edited question, in your View2 viewController, you've declared labelNumber1 as String and not as UILabel, so whenever you pass the value to labelNumber1, the value will be containing in this variable and it will not be shown in the screen since it is just a normal string. So what you can do is, declare the labelNumber1 as UILabel

class View2: UIViewController {

  @IBOutlet var labelNumber1: UILabel!

.....
}

and in your prepare for segue in ViewController1 make the following change,

class ViewController1: UIViewController {
.....

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

    // You can access the VC2 instance from the segue
      if let view2 = segue.destinationViewController as? VC2 {

        view2.labelNumber1.text = lblResult.text

      }
}

However if you still want to keep labelNumber1 as String, and to verify whether the value has been passed from ViewController1 during segue, add below line to viewDidLoad() of View2 ViewController.

class View2: UIViewController {
.....
   override func viewDidLoad() {
    super.viewDidLoad()
    print("labelNumber1: \(labelNumber1)")
   }
}

And you can see the printed value of labelNumber1 in the console.

HTH :)

iamyogish
  • 2,372
  • 2
  • 23
  • 40
1

You already have a segue between ViewController1 and ViewController2 it seems, so now you just need to pass some date from one to another. This can be done in the function prepare(for segue: UIStoryboardSegue, sender: AnyObject?) which is called when you transition from ViewController1 to ViewController2.

The UIStoryboardSegue class has a property called source of type UIViewController and another property called destination which is also a UIViewController.

So, if you define a property on your ViewController2, like so:

var labelValue: String

Then you can pass a value to it in your prepareForSegue defined on ViewController1 like so:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let viewController2 = segue.destination as? YourViewController2 else {
        return
    }
    viewController2.labelValue = theValueFromViewController1
}

Here is tutorial telling you a bit more (in Objective C though)

And this is also a fine introduction.

Hope that helps you.

pbodsk
  • 6,787
  • 3
  • 21
  • 51
  • 1
    OK. You are almost there. Now you have passed the value from ViewController1 to ViewController2 but you still need to show it on the UI. So in viewWillAppear for instance, you can set the value of Number1V2.text to labelNumber1 – pbodsk Nov 15 '16 at 19:21
  • Hey not sure if you can help me with this one as well please. Thanks http://stackoverflow.com/questions/40618827/give-values-to-buttons-swift/40619172?noredirect=1#comment68474362_40619172 – Luke_i Nov 16 '16 at 08:20
1

I am passing textFiled data to destViewController to show how segue performs when passing data.

Note: If you want to pass string data to your destVC.You can assign your string like var someValue: String

mainStoryBoard:

enter image description here

MainVC:

  @IBOutlet weak var textField: UITextField! 
  //or you can assign string like var someValue: : String

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let viewController = segue.destination as! destVC
        viewController.dataText = textField.text // someValue.text
  }

DestVC:

@IBOutlet var label: UILabel!

var dataText = String()
override func viewDidLoad()      
label.text = dataText                 
}

Output:

enter image description here

Joe
  • 8,868
  • 8
  • 37
  • 59