A.J. A.J. - 4 years ago 89
Swift Question

Programmatically generated buttons can't be pressed when the app is restarted

I have programmatically generated a series of buttons in my app, and they work perfectly when they are pressed. Then I stored in memory the number of the buttons and, when I restart the app, the buttons are regenerated exactly as they were before, and yet they can't be pressed.
The method "deletebtn" prints "pressed" when the button is pressed, but it doesn't.
I've even tried to add to the properties of the programmatically generated button "buttonName.userInteractionEnabled = true", but nothing changes.

This are the properties of the button, and that's also the code I have on the viewdidload, that recreates as much buttons as the number of them that I have stored in memory:

let btn: UIButton = UIButton(frame: CGRectMake(10, CGFloat(60*a), ScreenWidth - 20 , 50))
btn.layer.cornerRadius = 0.5 * btn.bounds.size.height
btn.backgroundColor = UIColor.whiteColor()
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.setTitle(ArrayService[(index)], forState: UIControlState.Normal)
btn.addTarget(self, action: Selector("deletebtn:"), forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = a //an int variable that is increased each time i create a neew button
btn.userInteractionEnabled = true

And here is the method deletebtn:

func deletebtn(sender:UIButton){

The scrollView is where I put my buttons. I've tried adding "ScrollPwd.userInteractionEnabled = true", and yet I can just scroll through the buttons, but I can't press them.

Can you help me?

Answer Source

I managed to solve it. It's pretty awkward actually: what happens it's just that when you restart the app the logs don't appear anymore.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download