ios - Adding UIButton dynamically: action not getting registered -
i'm creating pages dynamically, each page contains navigation controller , uiviewcontroller.
inside each page, there components link, images, texts.
each component class following:
class link: component, componentprotocol { var text: string var url: string func browseurl(sender: uibutton!){ let targeturl = nsurl(fileurlwithpath: self.url) let application = uiapplication.sharedapplication() application.openurl(targeturl!) } func generateview() -> uiview?{ var result: uiview? var y = cgrectgetminy(frame) var linkbtn = uibutton(frame: cgrect(x: 0, y:30 , width:300 , height: 50) linkbtn.settitle(self.text, forstate: uicontrolstate.normal) linkbtn.settitlecolor(uicolor.bluecolor(), forstate: uicontrolstate.normal) linkbtn.titlelabel?.font = linkbtn.titlelabel?.font.fontwithsize(15) // doesn't seem registered linkbtn.addtarget(self, action: "browseurl:", forcontrolevents: uicontrolevents.touchupinside) result = uiview() result?.addsubview(linkbtn) return result }
then in viewdidload of page viewcontroller method have initialise page components:
component in components!{ if let acomponent:componentprotocol = component as? componentprotocol { if let res = acomponent.generateview(innerframe) { if let view = res.view { self.view.addsubview(view) } } } }
the button showing, when touch nothing happens. when debugged, browseurl no triggered @ all.
what's wrong code? i'm guessing because registered action in link
class , not in viewcontroller of page?
update
this similar issue, answer not straightforward , have component reference in viewcontroller: (target: object) not working setting uibutton outside viewcontroller
i'm not @ mac right can't test, work:
linkbtn.target = self linkbtn.action = "browseurl:"
i think if have selectors in swift have careful colons , specifying @objc if class doesn't inherit nsobject, that?
Comments
Post a Comment