skip to Main Content

I have a TextLabel in the ViewController VC, which will receive the input of the user whenever the user has put text on there. Then, pressing a button, that text that was on the TextLabel, will pass onto a Label at the SecondaryView VC. But the thing is that I have tried multiple ways to set the text from the TextLabel to the Label on the SecondaryView VC.

This is the first way I tried:

This is my ViewController.swift file.


class ViewController: UIViewController {
    
    var text: String = ""
    
    @IBOutlet weak var mainViewTextLabel: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.destination is SecondaryView {
                let vc = segue.destination as? SecondaryView
                vc?.text = "(mainViewTextLabel!)"
            }
        }
    }
    

    @IBAction func onButtonTap(_ sender: Any) {
        
    }
    
}

And this is my SecondaryView.swift file:

import UIKit

class SecondaryView: UIViewController {
    
    var text:String = ""
    
    @IBOutlet weak var secondaryViewLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        secondaryViewLabel?.text = text
    }
    
}

When I run the app and type any text in the TextField and press the Button to go to the SecondaryView VC, there is no text on there.

If someone knowns another way to pass text from a View to another, or a way that the text can appear, I would appreciate it.

2

Answers


  1. It looks like you are not passing through the text of the UITextField. Instead you are passing through a reference to the UITextField as a string.
    What you want to do is access the .text property:

    vc?.text = mainViewTextLabel.text!
    

    The .text property returns a String optional or String? and it is generally bad practice to force unwrap it, since it could crash your application. Instead, you can use a guard/let statement to make sure it is not null. So:

    vc?.text = "(mainViewTextLabel!)"
    

    can be replaced by:

    guard let textFromTextField = mainViewTextLabel.text else {
        return
    }
    
    vc?.text = textFromTextField
    
    Login or Signup to reply.
  2. You have a couple of issues, I think.

    Firstly, you are calling prepare(for:...) within viewDidLoad. This function isn’t something you call yourself. It’s something that you provide an implementation for and the system calls it just before the segue.

    The second is that you are passing a reference to a UITextField rather than the text of that text field.

    Maybe something like this would be better:

    class ViewController: UIViewController {
        
        var text: String = ""
        
        @IBOutlet weak var mainViewTextLabel: UITextField!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // Do anything else you need to do when loading
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                if segue.destination is SecondaryView {
                    let vc = segue.destination as? SecondaryView
                    vc?.text = mainViewTextLabel.text ?? ""
                }
            }  
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search