ios - handle detail page for table view controller in swift -
i have table view controller cells. on clicking cell load view controller , want handle elements on view controller. on detail view controller placed label , in first step want set text of label, exception fatal error: unexpectedly found nil while unwrapping optional value
, don't know why. there solutions?
this part of table view controller on clicking cell:
override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { allow vcmissiondetail : viewcontrollermissiondetail = self.storyboard?.instantiateviewcontrollerwithidentifier("missiondetail") viewcontrollermissiondetail; //load detail view controller self.presentviewcontroller(vcmissiondetail, animated: true, completion: nil) //set label text //at line exception -> label nil vcmissiondetail.label.text = "test" }
and detail view controller (very simple):
import uikit class viewcontrollermissiondetail: 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. } }
thx!
instantiateviewcontrollerwithidentifier
returns optional because may fail due several reasons. should improve disclose conditionally:
override func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { if allow vcmissiondetail : viewcontrollermissiondetail = self.storyboard?.instantiateviewcontrollerwithidentifier("missiondetail") viewcontrollermissiondetail { //set label text before presenting viewcontroller vcmissiondetail.label.text = "test" //load detail view controller self.presentviewcontroller(vcmissiondetail, animated: true, completion: nil) } }
further things double check:
is viewcontrollermissiondetail
set custom class viewcontroller in identity inspector in interfacebuilder?
if vcmissiondetail
instantiated , still crashes delete label's outlet connection in interfacebuilder , recreate it.
ios uitableview swift uiviewcontroller
No comments:
Post a Comment