Joker
Joker

Reputation: 830

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) on uitextfield

I have a LoginViewController with viewDidLayoutSubviews() function and inside the function i have this code to create bottom only border for text field,

let txt_border = CALayer()
        let txt_pass_border = CALayer()
        let border_width = CGFloat(2.0)
        txt_border.borderColor = UIColor.grayColor().CGColor
        txt_pass_border.borderColor = UIColor.grayColor().CGColor
        txt_border.frame = CGRect(x: 0, y: txt_login_Email.frame.size.height - border_width, width: txt_login_Email.frame.size.width, height: txt_login_Email.frame.size.height)
        txt_pass_border.frame = CGRect(x: 0, y: txt_login_Password.frame.size.height - border_width, width: txt_login_Password.frame.size.width, height: txt_login_Password.frame.size.height)
        txt_border.borderWidth = border_width
        txt_pass_border.borderWidth = border_width
        txt_login_Email.layer.addSublayer(txt_border)
        txt_login_Email.layer.masksToBounds = true

        txt_login_Password.layer.addSublayer(txt_pass_border)
        txt_login_Password.layer.masksToBounds = true

but while running the app it shows an error output Console

and highlights the code enter image description here

what need to do to change this error ???

Upvotes: 0

Views: 319

Answers (2)

Joker
Joker

Reputation: 830

So wandering in search of solution in SO i got an answer for my Question from here . The Answer by Rob. i have added

let loginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginVC") as! LoginViewController

inside Appdelegate.swift files

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 let loginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginVC") as! LoginViewController
    }

And the Problem that was faced went gone.

The Problem started when i started using MMDrawerController from a Tutorial. As a beginner in iOS just followed his instructions and find myself in a Pit . just Blaming Myself for my stupidness. And now i need to do the Drawer Stuff again. Hope it helps someone someday.

Upvotes: 0

dirtydanee
dirtydanee

Reputation: 6151

If you override a function, you will have to always call its super. In this case this should be your first line instead of print("border")

super.viewDidLayoutSubviews()

Once this is done, check if

txt_login_Email 

is not nil.

Upvotes: 1

Related Questions