ios - How to add action to a subviews Attributes Swift? -
this question has answer here:
i relatively new swift , add series of action custom uiview
button
here's code when ever press button error.
please tell me best practice modelling this.
class cameraview: uiview { var camerabutton : uibutton? convenience init() { self.init(frame: cgrectzero) self.backgroundcolor = uicolor.clearcolor() camerabutton = uibutton() required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } override init(frame: cgrect) { super.init(frame: frame) } func setbutton(button : uibutton){ self.camerabutton = button } func setupmyview(){ self.camerabutton = uibutton() self.camerabutton?.layer.cornerradius = 30 self.camerabutton?.layer.borderwidth = 5 self.camerabutton?.backgroundcolor = uicolor.clearcolor() self.camerabutton?.layer.bordercolor = uicolor.whitecolor().cgcolor self.addsubview(camerabutton!) } } viewcontroller.swift import uikit class viewcontroller: uiviewcontroller { var cameraview : cameraview? override func viewdidload() { super.viewdidload() cameraview = cameraview() cameraview?.camerabutton?.addtarget(self, action: "pressed", forcontrolevents: .touchupinside) self.view = cameraview } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func pressed(sender : anyobject!){ print("button pressed") let alert = uialertcontroller(title: "alert", message: "another alert", preferredstyle: .alert) let action = uialertaction(title: "ok", style: .default, handler: nil) alert.addaction(action) self.presentviewcontroller(alert, animated: true , completion: nil) } }
error
2016-01-16 14:00:42.023 tpproject[4194:1442964] -[tpproject.viewcontroller pressed]: unrecognized selector sent instance 0x125655a30 2016-01-16 14:00:42.028 tpproject[4194:1442964] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[tpproject.viewcontroller pressed]: unrecognized selector sent instance 0x125655a30' *** first throw call stack: (0x183d39900 0x1833a7f80 0x183d4061c 0x183d3d5b8 0x183c4168c 0x188a63e50 0x188a63dcc 0x188a4ba88 0x188a636e4 0x188a63314 0x188a5be30 0x188a2c4cc 0x188a2a794 0x183cf0efc 0x183cf0990 0x183cee690 0x183c1d680 0x18512c088 0x188a94d90 0x1000adf64 0x1837be8b8) libc++abi.dylib: terminating uncaught exception of type nsexception
i figured out solution. had add : action
from line
cameraview?.camerabutton?.addtarget(self, action: "pressed", forcontrolevents: .touchupinside)
to
cameraview?.camerabutton?.addtarget(self, action: "pressed:", forcontrolevents: .touchupinside)
Comments
Post a Comment