简体   繁体   中英

Cannot assign value of type 'String?' to type 'UILabel?'

I've just started with programming, and now I try to work with segues and I copy one example: open a new screen with the transfer of text from the text field, then exit this screen to the main one with the transfer of information from the second screen, which is the same text. When I open a finished project, it works great, but mine is not. code is the same, all outlets, actions and segues are the same. And this error I get if I make segues through the screen. Guys, please, help)) FIRST VC

class ViewController: UIViewController {

@IBOutlet weak var loginTF: UITextField!
@IBOutlet weak var passwordTF: UITextField!
@IBOutlet weak var resultLabel: UILabel!

@IBAction func loginTapped(_ sender: UIButton) {
    performSegue(withIdentifier: "detailSegue", sender: nil)
}
@IBAction func unwindSegueToMainScreen(segue: UIStoryboardSegue){
    guard let svc = segue.source as? SecondViewController else { return }
    self.resultLabel = svc.label.text
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let dvc = segue.destination as? SecondViewController else { return }
    dvc.login = loginTF.text
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

} SECOND VC

class SecondViewController: UIViewController {
var login: String?
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
    guard let login = self.login else { return }
    label.text = "Hello, \(login)"
}
@IBAction func goBackTapped(_ sender: UIButton) {
}  
}

Change this

self.resultLabel = svc.label.text

to

self.resultLabel.text = svc.label.text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM