ios - How do i pass information to a Uilabel in the second view controller in swift? -
i'm new swift , i'm trying learn different techniques.
the situation: have 2 view controllers. view controller number 1 consist of 4 buttons (north, south, east, west) example. lets click on north button. should take view controller number 2 , uilabel in view controller 2 should displaying name of whatever button pressed ("north" in case). know when you're passing information forward, should use "prepare segue" method there way 4 buttons? have optional string variable in view controller 2 should catch information being passed view controller 1. i've searched everywhere haven't gotten answer on this.
the code have in view controller 1:
@ibaction func north(sender: uibutton) { } @ibaction func east(sender: uibutton) { } @ibaction func west(sender: uibutton) { } @ibaction func south(sender: uibutton) { }
the code have in view controller 2:
@iboutlet weak var label2: uilabel! var updatethelabel: string? override func viewdidload() { super.viewdidload() label2.text = updatethelabel! }
question: how perform segue 4 buttons go second view controller , update uilabel respectively?
to add @ahmad-farrag's solution
you can modify ib actions pick text button pressed
var buttontext = "" @ibaction func north(sender: uibutton) { buttontext = sender.currenttitle.text } @ibaction func east(sender: uibutton) { buttontext = sender.currenttitle.text } @ibaction func west(sender: uibutton) { buttontext = sender.currenttitle.text } @ibaction func south(sender: uibutton) { buttontext = sender.currenttitle.text }
this assign text buttons buttontext
variable. in prepareforsegue
let assume 2 view controllers connected segue identifier secondcontrollersegue
.
override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "secondcontrollersegue" { let controller = segue.destinationviewcontroller as! secondviewcontroller controller.updatethelabel = buttontext } }
this send buttontext
have captured earlier secondviewcontroller
Comments
Post a Comment