Pass variable from child view to parent view in Swift -
i'm trying pass variable child view (inputpopup.swift) parent view using setter , getter functions. here code in child view:
var char: string! var buttonpressed = string() @ibaction func addbuttonpressed(sender: anyobject) { buttonpressed = "addbutton" setchar("+") getchar() self.removeanimate() } @ibaction func minusbuttonpressed(sender: anyobject) { buttonpressed = "minusbutton" setchar("-") self.removeanimate() } @ibaction func dividebuttonpressed(sender: anyobject) { buttonpressed = "dividebutton" setchar("/") self.removeanimate() } @ibaction func multiplybuttonpressed(sender: anyobject) { buttonpressed = "multiplybutton" setchar("*") self.removeanimate() } //setter method func setchar(var thischar: string){ char = thischar } //getter method func getchar()-> string{ if (buttonpressed == "addbutton"){ char = "+" } if (buttonpressed == "minusbutton"){ char = "-" } if (buttonpressed == "dividebutton"){ char = "/" } if (buttonpressed == "multiplybutton"){ char = "*" } return char }
i trying access 'char' variable in parent view so, returning nil - presumably because calling new instance of function.
@ibaction func runbuttonpressed(sender: anyobject) { inputpopup().getchar() }
how can pass data child parent effectively?
i update label in parent view, based on button click within child view. trying implement key-value observation. here's i've got far.
class observechar: nsobject { dynamic var char = string() func updatechar(){ char = string() }
}
private var mycontext = 0 class observer: nsobject { var objecttoobserve = observechar() override init(){ super.init() objecttoobserve.addobserver(self, forkeypath: "char", options: .new, context: &mycontext) } override func observevalueforkeypath(keypath: string, ofobject object: anyobject, change: [nsobject : anyobject], context: unsafemutablepointer<void>) { if context == &mycontext { println("char updated: \(change[nskeyvaluechangenewkey])") } else { super.observevalueforkeypath(keypath, ofobject: object, change: change, context: context) } } deinit { objecttoobserve.removeobserver(self, forkeypath: "char", context: &mycontext) }
}
ok,maybe can set child view property of parent view,maybe
var inputpopview : inputpopup /**<initialize somewhere */
and can use
@ibaction func runbuttonpressed(sender: anyobject) { self.inputpopview.getchar() }
Comments
Post a Comment