我有一个注册的viewController(VC)。用户提交注册详细信息后,我的远程服务器将以“注册成功”响应。收到此消息后,我可以提出一个新的VC,要求用户检查他们的电子邮件,以验证他们的电子邮件地址。我能够呈现新的视图控制器,但未能将用户的电子邮件地址传输到新的VC。我的代码如下:

     guard
          let registeredEmail = myArray["email"] as? String else{
          print("cannot find email address")
          return
          }

          let verifyEmailVC = self.storyboard?.instantiateViewController(withIdentifier: "emailConfirmVC") as! VerifyEmailViewController
          print("registeredEmail: \(registeredEmail)")
          verifyEmailVC.emailToVerify.text! = registeredEmail
          self.present(verifyEmailVC, animated: true, completion: nil)

类验证电子邮件视图控制器:
    class VerifyEmailViewController: UIViewController {

    @IBOutlet var emailToVerify: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

慰问:
 jsonObject: {
email = "[email protected]";
firstname = Wang;
lastname = tran;
message = "registration_success";
status = 200;
userId = 53;
}
message: registration_success
registeredEmail: [email protected]

我得到以下错误:
线程1:致命错误:在展开
可选值
排队:verifyEmailVC.emailToVerify.text! = registeredEmail

最佳答案

VerifyViewController中的Label为nil,您正在尝试设置该lable的值。尝试像这样将字符串传递给控制器

let verifyEmailVC = self.storyboard?.instantiateViewController(withIdentifier: "emailConfirmVC") as! VerifyEmailViewController
print("registeredEmail: \(registeredEmail)")
verifyEmailVC.email = registeredEmail
self.present(verifyEmailVC, animated: true, completion: nil)


class VerifyEmailViewController: UIViewController {

@IBOutlet var emailToVerify: UILabel!
var email: String?

override func viewDidLoad() {
    super.viewDidLoad()
    emailToVerify.text = email
}

关于ios - 在没有Segues的ViewController之间传输数据-Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47435546/

10-14 20:45