ios - How to solve exception with Xcode 6.1 with fatal error: expectedly found nil while unwrapping an Optional value -



ios - How to solve exception with Xcode 6.1 with fatal error: expectedly found nil while unwrapping an Optional value -

i have problem swift in xcode since 2 days , don't know answer. searched problem i've found no solution works me. 2 days ago did alter because of problem, see link.

now have problem exception message unexpectedly found nil while unwrapping optional value when want handle element of view controller. in little illustration have 2 view controllers (viewcontroller, viewcontroller2). on viewcontroller added button , on viewcontroller2 added label. both elements referenced correctly. on clicking button on viewcontroller want print text of label of viewcontroller2. fatal error: unexpectedly found nil while unwrapping optional value.

maybe have general problem xcode 6.1? how can solve problem?

here code viewcontroller:

class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func onclick(sender: anyobject) { allow view2 = self.storyboard?.instantiateviewcontrollerwithidentifier("view2") viewcontroller2; //in line fatal error println(view2.label.text) } }

here code viewcontroller2:

class viewcontroller2: uiviewcontroller { @iboutlet var label: uilabel! override func viewdidload() { super.viewdidload() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }

calling instantiateviewcontrollerwithidentifier gets instance of viewcontroller2 -- doesn't load vc's view storyboard. (it's same calling init(nibname:bundle: on vc class.) such, @iboutlet variables haven't been hooked yet. when seek reference one, nil, , since you're referencing through implicitly unwrapped optional , trying access 1 of properties, "unexpectedly found nil while unwrapping optional value" crash.

@iboutlet variables guaranteed have been loaded of viewdidload. if you're instantiating vc through instantiateviewcontrollerwithidentifier, you'll need tell load view (by accessing view property) before can access of outlets.

ios xcode swift uiviewcontroller fatal-error

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -