Luke_i
Luke_i

Reputation: 163

Show Label Value on 2nd View - Swift

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

Upvotes: 0

Views: 1244

Answers (3)

iamyogish
iamyogish

Reputation: 2432

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 :)

Upvotes: 1

Joe
Joe

Reputation: 8986

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

Upvotes: 1

pbodsk
pbodsk

Reputation: 6876

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.

Upvotes: 1

Related Questions