Swift: UIButton not calling correctly -


here code (in swift 2) creating ui button.

let settingsbtn = uibutton(type: uibuttontype.system) uibutton settingsbtn.frame = cgrect(x: 50, y: 50, width: 40, height: 40) settingsbtn.center = cgpoint(x: view.center.x * 1.8, y: view.center.y * 1.85) settingsbtn.setimage(uiimage(named: "gear"), forstate: uicontrolstate.normal) settingsbtn.addtarget(self, action: selector("settings"), forcontrolevents: uicontrolevents.touchupinside) self.view?.addsubview(settingsbtn) 

in function have this:

playbtn.removefromsuperview() charbtn.removefromsuperview() settingsbtn.removefromsuperview() 

it removes throws me error when coming settings.btn.

not sure why, maybe because way ui button created? code button found creating button custom image.

my error below:

fatal error: unexpectedly found nil while unwrapping optional value (lldb)  

although defined var settingsbtn : uibutton! outside class; variable settingsbtn not highlighted rest of them.

it's hard know sure code, seems me settingsbtn going out of scope. sure it's added view? try , placing breakpoint on :

self.view?.addsubview(settingsbtn) 

and checking if view can unwrapped or if it's nil.

perhaps post rest of code can clearer picture?


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -