ios - Passing Data between interface controllers in WatchKit -
how pass data stored in label interface controller interface controller in watchkit using swift? can't seem find answer anywhere, hope here can me. i've included section of code deals calculating value need passed. want show same value calculated user , elaborate more on in next interface controller named resultscontroller. appreciated :d
class interfacecontroller: wkinterfacecontroller { var currentvalue: string = "0" func setdisplayvalue(value: double) { var percent = value // check if value integer if value % 1 == 0 { // our value integer currentvalue = "\(int(value))"// new value % } else { // our value float currentvalue = "\(value)" } // display 2 decimal places in final amount var round = nsstring(format:"%.2f", value) displaylabel.settext("$\(round)") // display final value // value want passed label in ic. } // answer tapped @ibaction func totaltapped() { if command != nil { let answer = command!.executewithnewvalue((currentvalue nsstring).doublevalue) setdisplayvalue(answer) command = nil calculationexecuted = true } } }
this second interface controller want value first 1 displayed in using label.
import watchkit import foundation class resultscontroller: wkinterfacecontroller { @iboutlet weak var totallabel: wkinterfacelabel! override func awakewithcontext(context: anyobject?) { super.awakewithcontext(context) // label hold same value in previous interface controller totallabel.settext("") } override func willactivate() { // method called when watch view controller visible user super.willactivate() } override func diddeactivate() { // method called when watch view controller no longer visible super.diddeactivate() } }
edit: here tutorial followed, manipulated bit tip cal. added in resultscontroller hold information inputted user using calculator interfacecontroller can't seem pass data on labels in rc, in command removed subtract , divide since wouldn't needing those. calculation buttons have multiply , addition. example of how should work: enter in amount 12.58 tap multiply , enter in 15 tap add , displays final amount of 14.46 need pass on of values rc in separate labels.
here trying accomplish: passing initial amount label, , tip amount, final cost separate labels resemble bill.
the best way override contextforseguewithidentifier
method called when segue 1 view next.
the method of passing data between wkinterfacecontroller
s bit different method doing uiviewcontroller
s in traditional ios app.
normally, you'd this:
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "showdetail" { let controller: detailviewcontroller = (segue.destinationviewcontroller uinavigationcontroller).topviewcontroller detailviewcontroller controller.myvariable = myvalue } }
however, there no prepareforsegue
method in watchkit. contextforseguewithidentifier
wkinterfacecontroller
method similar, has glaring difference: doesn't provide instance of destination view controller.
instead, return data method, , access data within target class.
so, in case, this:
// in interfacecontroller override func contextforseguewithidentifier(segueidentifier: string) -> anyobject? { // may want set context's identifier in interface builder , check here make sure you're returning data @ proper times // return data accessed in resultscontroller return self.currentvalue } // in resultscontroller override func awakewithcontext(context: anyobject?) { super.awakewithcontext(context) // make sure data passed , update label accordingly if let val: string = context as? string { self.totallabel.settext(val) } else { self.totallabel.settext("") } }
Comments
Post a Comment