I am not finding text here in this code below

class SecondViewController: UIViewController {

    @IBOutlet weak var myNameTextField: UITextField!

   

    

    @IBOutlet var myNameLabel: UIView!

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    

    @IBAction func onSubmitClick(_ sender: Any) {

        

        if(myNameTextField.text != ""){

            myNameLabel.text=myNameTextField.text

         }

    }

    

Answered by Claude31 in 719775022

You are probably new to the forum. When you paste code, please use Paste and Match Style, then use Code formatter tool (<>)

class SecondViewController: UIViewController {

    @IBOutlet weak var myNameTextField: UITextField!
    @IBOutlet var myNameLabel: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func onSubmitClick(_ sender: Any) {
        
        if(myNameTextField.text != "") {
            myNameLabel.text = myNameTextField.text
         }
    }

You declared myNameLabel as UIView

Most likely, you need a UILabel:

    @IBOutlet var myNameLabel: UILabel!

To change:

  • first disconnect the label in Storyboard from its IBOutlet (right-click on the label in storyboard, then click the small x in front of Referencing outlet)
  • change in code UIView to UILabel
  • reconnect the label to its IBOutlet
  • do a clean Build Folder (Product Menu) to clean evrything

That should work now.

If that do not work, please tell exactly what error you get. If that works, don't forget to close the thread by marking the correct answer.

Please help

getting error in myNameLabel.text=myNameTextField.text // error

Accepted Answer

You are probably new to the forum. When you paste code, please use Paste and Match Style, then use Code formatter tool (<>)

class SecondViewController: UIViewController {

    @IBOutlet weak var myNameTextField: UITextField!
    @IBOutlet var myNameLabel: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func onSubmitClick(_ sender: Any) {
        
        if(myNameTextField.text != "") {
            myNameLabel.text = myNameTextField.text
         }
    }

You declared myNameLabel as UIView

Most likely, you need a UILabel:

    @IBOutlet var myNameLabel: UILabel!

To change:

  • first disconnect the label in Storyboard from its IBOutlet (right-click on the label in storyboard, then click the small x in front of Referencing outlet)
  • change in code UIView to UILabel
  • reconnect the label to its IBOutlet
  • do a clean Build Folder (Product Menu) to clean evrything

That should work now.

If that do not work, please tell exactly what error you get. If that works, don't forget to close the thread by marking the correct answer.

Thank you Claude. Really appreciate your help. Can you please help me with this "When you paste code, please use Paste and Match Style, then use Code formatter tool (<>)

" I did not get this point.

You see how the code looks when properly formatted. Much easier to read.

To paste code, you usually first copy in Xcode.

  • To avoid the forum editor adding extra blank lines, instead of just Pasting text, use the Safari menu Edit > Paste and Match Style.
  • then select the whole code section and click on code formatter tool ( </>) at the bottomof the reply area.

You will get a clean code presentation.

I am not finding text here in this code below
 
 
Q