简体   繁体   English

无法分配“字符串”类型的值? 输入“UILabel?”

[英]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.我刚刚开始编程,现在我尝试使用 segues 并复制一个示例:打开一个新屏幕,从文本字段传输文本,然后退出该屏幕到主屏幕,从第二个屏幕,这是相同的文本。 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伙计们,请帮忙))第一个 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第二个 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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