Swift: How to pass a Parameter to a function which itself is also a Parameter? -
it's this:
var = 0 button.addtarget(self, action:"showcontent", forcontrolevents: uicontrolevents.touchupinside) and function "showcontent" this:
func showcontent(i: int) { //do sth. } i want pass variable function showcontent when button touched, how ?
in target-action pattern, can't that. can either use "showcontent", in case function be:
func showcontent() { } … or can add colon ("showcontent:") in case function be:
func showcontent(sender : uibutton) { // sender } this helps enforce broader model / view / controller pattern making difficult views "smart". instead, model , controller objects should handle i. button displays it's told, , tells controller when it's tapped.
you can read i, , respond it, in appropriate method. example:
class viewcontroller : uiviewcontroller { var = 0 var button = uibutton(type: .system) var label = uilabel() override func viewdidload() { button.addtarget(self, action:"showcontent", forcontrolevents: uicontrolevents.touchupinside) } func showcontent() { i++ label.text = "current value of i: \(i)" } }
Comments
Post a Comment