ios - SWIFT - Property not seen outside viewdidload -
i try pass string vc1 vc2, value of tex passed property detailitem loaded viewdidload, , not in function
viewcontroller 1:
@iboutlet var tex: uitextfield! // prepare fore segue pass value of txt override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier? == "todo" { let navvc = segue.destinationviewcontroller uinavigationcontroller let itemvc: checklistviewcontroller = navvc.topviewcontroller checklistviewcontroller itemvc.detailitem! = tex.text } }
viewcontroller 2:
var detailitem : string! // can see value of tex in viewdidload override func viewdidload() { super.viewdidload() println("viewdidload \(detailitem)") tableview.rowheight = 44 } // here , in function below result nil. required init(coder adecoder: nscoder) { println("print init \(detailitem)") items = [checklistitem]() super.init(coder: adecoder) loadchecklistitems() } func documentsdirectory() -> string { let paths = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true) [string] return paths[0] } func datafilepath() -> string { println("print path\(detailitem)") return documentsdirectory().stringbyappendingpathcomponent("checklists.plist") } func savechecklistitems() { let data = nsmutabledata() let archiver = nskeyedarchiver(forwritingwithmutabledata: data) archiver.encodeobject(items, forkey: "checklistitems") archiver.finishencoding() data.writetofile(datafilepath(), atomically: true) println("print save\(detailitem)") } func loadchecklistitems() { let path = datafilepath() if nsfilemanager.defaultmanager().fileexistsatpath(path) { if let data = nsdata(contentsoffile: path) { println("print load\(detailitem)") let unarchiver = nskeyedunarchiver(forreadingwithdata: data) items = unarchiver.decodeobjectforkey("checklistitems") [checklistitem] unarchiver.finishdecoding() } } }
console output:
print init print path print load viewdidload antwerp
thanks alberto
you can't see property in initializer because code called before itemvc.detailitem! = tex.text
.
you move call loadchecklistitems()
viewdidload()
. load items detail item set:
var detailitem : string! { didset { loadchecklistitems() } }
Comments
Post a Comment