2016-09-07 163 views
-1

我正在使用數組中的數據創建按鈕。它應該看起來像下面這樣:iPad中的按鈕無法點擊

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)") 
    } 

使用上面的代碼中,幾個按鈕是可以點擊的,但有些不是。不知道是什麼導致按鈕不能工作。你能幫我嗎?

回答

4

這是正常的,因爲你只是不啓動索引爲2和3

button.enabled=false 

^所有其他按鈕,這是「什麼導致按鈕不工作」

+0

偉大捕獲...... ... –