ios - Calling Other ViewController opens a black screen -
i have begun development of ios in swift
, , stuck on 1 thing. want pass string value 1 viewcontroller
other.
1st view controller
// on tablecell click func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { commonfunctions.setdefaults("a", value: arr[indexpath.row]) let viewcontroller = secondview(passeddata:arr[indexpath.row]) self.presentviewcontroller(viewcontroller, animated: true, completion: nil) }
2nd viewcontroller
var test:string init(passeddata:string){ self.test = passeddata super.init(nibname: nil, bundle: nil) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } override func viewdidload() { super.viewdidload() print(commonfunctions.getdefaults("a")) }
i getting string in 2nd viewcontroller
problem getting black screen.
you getting black screen because initialising secondviewcontroller
code let viewcontroller = secondview(passeddata:arr[indexpath.row])
.
this invoking custom initalizer created not loading view
property of secondviewcontroller
.
there several approaches solve this:
if using storyboard should instead of manually initializing view controller use segue , pass data on prepareforsegue
.
performseguewithidentifier("the identifier of segue on storyboard", sender: nil) override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "the identifier of segue on storyboard" { let secondviewcontroller = segue.destinationviewcontroller as! secondviewcontroller secondviewcontroller.passeddata = //data } }
if using xibs should use
let secondviewcontroller = secondviewcontroller(nibname: "name of nib file", bundle: nsbundle.mainbundle()) secondviewcontroller.passeddata = //data
or add on custom initialiser nib name of nib:
init(passeddata:string){ self.test = passeddata super.init(nibname: /*add nib name here*/, bundle: nil) }
Comments
Post a Comment