dang dang - 2 months ago 8
iOS Question

Buttons in iPad not clickable

I am creating buttons using data from array. It should look like the following:

enter image description here

override func viewDidLoad()
{
super.viewDidLoad()

var xMargin:CGFloat = 20.0
var yTopMargin:CGFloat = 40.0


var i = 1


let DataTanks = ["Button 1", "Button 2", "Button 3", "Button 4", "Button 5", "Button 6"]

for index in 0...5
{
let button = UIButton()
button.tag=index
var buttonFrame = self.view.frame
buttonFrame.origin.x += xMargin
buttonFrame.origin.y += yTopMargin
buttonFrame.size.width = 200
buttonFrame.size.height = 200

button.frame = buttonFrame
button.layer.cornerRadius = 15.0
button.layer.shadowColor = UIColor(red: 64/255, green: 64/255, blue: 64/255, alpha: 1).CGColor
button.layer.shadowOpacity = 0.5
button.layer.shadowRadius = 5
button.layer.shadowOffset = CGSizeMake(2.0, 2.0)
button.layer.zPosition = 10

button.backgroundColor = UIColor.lightGrayColor()
button.enabled=false

if(index==2)
{
button.enabled=true
button.backgroundColor = UIColor(red: 153/255, green: 0/255, blue: 0/255, alpha: 1.0)
}
if(index==3)
{
button.enabled=true
button.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 102/255, alpha: 1.0)
}

button.setTitle(DataTanks[index], forState: UIControlState.Normal)
button.titleLabel!.numberOfLines = 3;
button.addTarget(self, action: #selector(myTestViewController.didTouchButton), forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button)


xMargin+=250.0

i+=1

if(i > 3 )
{
yTopMargin+=300.0
xMargin=20.0

i=1
}
}

}


func didTouchButton(sender:UIButton!) {

print("Button - \(btnsendtag.tag)")
}


Using the above code, a few buttons are clickable but others are not. Not sure what's causing the button not to work. Can you help?

Answer

This is normal because you disactivated all other buttons except with index 2 and 3.

button.enabled=false

^ this is 'what's causing the button not to work'

Comments