swift - Increase a counter in the first interface controller by pressing a button in the second interface controller -
i increase value of property in first interface controller in ibaction (add1) method of second interface controller
and use value of property update label when first view controller activated
i able increase value , value increases if press button.
i need find solution when press ibaction in second interface controller , can result , use in first interface controller , update label.
here code:
first interface controller:
blockquote
@iboutlet weak var resultbuttonlabel: wkinterfacebutton! @ibaction func resultbutton() { pushcontrollerwithname("secondinterfacecontroller", context: self) } override func willactivate() { super.willactivate() resultbuttonlabel.settitle("\(counter++)") }
blockquote
second interface controller:
blockquote
var counter = 1 @ibaction func wescored() { counter++ popcontroller() }
blockquote
the easy way implement idea use of nsuserdefaults
in firstviewcontroller can read values form nsuserdefaults
way:
override func viewdidload() { super.viewdidload() let score = nsuserdefaults().integerforkey("score") resultbuttonlabel.text = "\(score)" }
and secondviewcontroller can increase counter nsuserdefaults
way:
import uikit class secondviewcontroller: uiviewcontroller { var counter = int() override func viewdidload() { super.viewdidload() counter = nsuserdefaults().integerforkey("score") } @ibaction func wescored(sender: anyobject) { counter++ nsuserdefaults().setinteger(counter, forkey: "score") let storyboard = uistoryboard(name: "main", bundle: nil) let vc = storyboard.instantiateviewcontrollerwithidentifier("viewcontroller") as! uiviewcontroller self.presentviewcontroller(vc, animated: true, completion: nil) } }
hope help.
Comments
Post a Comment