Friday 15 April 2011

ios - Add button only in Code? -



ios - Add button only in Code? -

i'm trying add together button in code. in xcode simulator works not on device :

fatal error: unexpectedly found nil while unwrapping optional value

my code:

@iboutlet weak var playbutton: uibutton! override func viewdidload() { super.viewdidload() allow image = uiimage(named: "playbutton.png") uiimage playbutton = uibutton.buttonwithtype(uibuttontype.system) uibutton playbutton.frame = cgrectmake(10, 10, 100, 100) // crash here playbutton.center = cgpointmake(self.frame.width/2, self.frame.height/1.7) playbutton.addtarget(self, action: "transition:", forcontrolevents: uicontrolevents.touchupinside) playbutton .setbackgroundimage(image, forstate: uicontrolstate.normal) self.view?.addsubview(playbutton) }

someone can help me?

that makes no sense - if using @iboutlet means button created in storyboard, hence not need initialize it:

let image = uiimage(named: "playbutton.png") uiimage playbutton = uibutton.buttonwithtype(uibuttontype.system) uibutton playbutton.frame = cgrectmake(10, 10, 100, 100) // crash here playbutton.center = cgpointmake(self.frame.width/2, self.frame.height/1.7) playbutton.addtarget(self, action: "transition:", forcontrolevents: uicontrolevents.touchupinside) playbutton .setbackgroundimage(image, forstate: uicontrolstate.normal) self.view?.addsubview(playbutton)

remove of above code , set in storyboard instead. @ibaction should separate method hooked touchupinside of button in storyboard.

ios iphone swift uibutton

No comments:

Post a Comment