Swift class doesn't like self.view.addSubview() -
i've built custom button , i'm trying put in class.
the code below throws error line "self.view.addsubview(button)
" - used when code running under viewdidload
.
any suggestions?
class mybutton { var buttonlabel:string var buttonradius:cgfloat = 90 var button = uibutton.buttonwithtype(uibuttontype.system) uibutton init(buttoncolour:cgcolorref, buttonlabel:string){ self.buttoncolour = buttoncolour self.buttonlabel = buttonlabel } func drawbutton() { button.frame = 100, 100, buttonradius * 2, buttonradius * 2) button.layer.bordercolor = buttoncolour button.settitle(buttonlabel, forstate: uicontrolstate.normal) button.addtarget(self, action: "pressed:", forcontrolevents: uicontrolevents.touchupinside) self.view.addsubview(button) // error: 'viewcontroller.mybutton' not have member named 'view' }
you doing wrong. class not have view property. guess trying use view controller. every viewcontroller has property named view of type uiview looking for.
class myviewcontroller: uiviewcontroller { var buttonlabel:string var buttonradius:cgfloat = 90 var button = uibutton.buttonwithtype(uibuttontype.system) uibutton init(buttoncolour:cgcolorref, buttonlabel:string){ self.buttoncolour = buttoncolour self.buttonlabel = buttonlabel } func drawbutton() { button.frame = 100, 100, buttonradius * 2, buttonradius * 2) button.layer.bordercolor = buttoncolour button.settitle(buttonlabel, forstate: uicontrolstate.normal) button.addtarget(self, action: "pressed:", forcontrolevents: uicontrolevents.touchupinside) self.view.addsubview(button) // error: 'viewcontroller.mybutton' not have member named 'view' } }
Comments
Post a Comment