Poles
Poles

Reputation: 3682

fatal error: unexpectedly found nil while unwrapping an Optional value while editing textfield in swift

I'm facing a weird problem. I have a registration viewcontroller. From there I'm navigating to second viewcontroller after registration completion, where there is a simple textfield. Now when I tap on the textfield, after executing textFieldDidBeginEditing its gives fatal error.

fatal error: unexpectedly found nil while unwrapping an Optional value

Next thing I got stuck at the breakpoint

0xc7b108 <+68>:  bl     0xcd0198                  ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
0xc7b10c <+72>:  trap

Things I have checked so far.

  1. @IBOutlet is connected.
  2. textfield.delgate is set to self.
  3. textfield is not nil (Logged texfield variable at textFieldDidBeginEditing section)

Now the most interest thing is when I start the app again and navigate to the same viewcontroller (Second Viewcontroller), I can edit the textfield. No trap or crash. This thing is only happening while I pushing secondviewcontroller right after registration completion.

After registration I'm using this code to navigate second viewcontroller

let instanceViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(instanceViewController, animated: true)

Also I'm using the following code whose login credentials is saved at AppDelegate didFinishLaunchingWithOptions to navigate second viewcontroller

N.B: This code will only executes if a user terminates the app after successfull registration and try to launch the app again.

let userDefaults = NSUserDefaults.standardUserDefaults()
if let isUserLogin:Bool = userDefaults.boolForKey(ConstantsKey.UserDefault.isLogin){
    let objMainViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
    let navController = UINavigationController.init(rootViewController: objMainViewController)

    self.window?.makeKeyAndVisible()            
    self.window?.rootViewController = navController
}

Can anyone help me out?

Upvotes: 0

Views: 687

Answers (2)

Poles
Poles

Reputation: 3682

Sorry for the trouble. I think I figured it out.

It's happening because of the keyboard keyboardWillShow notification. Now the keyboardWillShow notification function is implemented in registration viewcontroller and I was also working with another textfield variable of the same viewcontroller. So when the keyboardWillShow executes for secondviewcontroller its found the registration page's textfield as nil which gives me fatal error.

Solution: I just had to remove observer from the registration viewcontroller viewcontroller in deinit function.

Upvotes: 1

Zahirul Islam
Zahirul Islam

Reputation: 147

let instanceViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as? SecondViewController

Upvotes: 0

Related Questions