0

I have a sign up viewController(VC). Once the user submits his registration details, my remote server responds with a 'registration_success'. Upon receiving this message, I am able to present a new VC that requests that the user check their email to verify their email address. I am able to present the new view controller but failing to transfer the user's email address to the new VC. My code is below:

     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:

    class VerifyEmailViewController: UIViewController {

    @IBOutlet var emailToVerify: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

console:

 jsonObject: {
email = "tran123@gmail.com";
firstname = Wang;
lastname = tran;
message = "registration_success";
status = 200;
userId = 53;
}
message: registration_success
registeredEmail: tran123@gmail.com

I am getting the following error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

in line: verifyEmailVC.emailToVerify.text! = registeredEmail

johnDoe
  • 709
  • 11
  • 29

3 Answers3

1

Label in the VerifyViewController is nil and you are trying to set value to that lable. Try passing string to your controller like this

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
}
Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
1

I have modified your code with few correction try below code

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

In VerifyEmailViewController

    class VerifyEmailViewController: UIViewController {

    var emailToVerifyStr = ""
    @IBOutlet var emailToVerify: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear(_ animated:Bool){
       super.viewWillAppear(true)
       self.emailToVerify.text = emailToVerifyStr
    }
  }

Hope that will help you :)

Ganesh Manickam
  • 2,113
  • 3
  • 20
  • 28
0

First of all, having JSON object used by your high level objects like ViewController is not something I would recommend.

You should consider having a "Store" object which receives the JSON, create a User object.

Then, you can create your one init function to pass your User object directly.

Like this :

- (instancetype)initWithUser(User *)user {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

   self = [storyboard instantiateViewControllerWithIdentifier:@"emailConfirmVC"];

   if(self) {
      //default initialization
   }

   return  self;
 }
marshallino16
  • 2,645
  • 15
  • 29