swift - Using Selectors as variables with addTarget -
swift - Using Selectors as variables with addTarget -
i having problems selectors in swift. have next lines of code:
let actiontarget="onpress:" button.addtarget(self, action: selector(actiontarget), forcontrolevents: .touchupinside)
but generates error:
! not find fellow member 'touchupinside'
however error disappears if try:
button.addtarget(self, action "onpress:", forcontrolevents: .touchupinside)
so assuming not .touchupinside
causing me problems, lack of string literal causing me problems.
i have tried:
let actiontarget="onpress:" selector button.addtarget(self, action: actiontarget, forcontrolevents: .touchupinside)
but didnt work either.
how can supply action selector not string literal, instead stored in variable?
button.addtarget(self, action: "onpress:", forcontrolevents: .touchupinside)
this one's right -- don't need special selector() utilize selector, using string do.
if selector stored in variable, utilize this:
let selector = "onpress:" button.addtarget(self, action: selector(selector), forcontrolevents: .touchupinside)
the reason as
isn't right here because it's used cast superclass subclass. example, let p:uiview = uibutton(...)
, p uibutton
work since p
in fact uibutton
instance. in case, using selector( ... )
calls appropriate selector constructor , instantiates new selector string.
swift
Comments
Post a Comment