2017-12-01 14 views

答えて

0

は、ソリューションは、すべてのボタン

var buttonArray = [UIButton]() 

func createButtons(){ 
    //create 5 buttons 
    for i in 1...5 { 
     let button = UIButton(frame: CGRect(x: 0, y: i*55, width: 130, height: 50)) 
     //set same background for each button 
     button.backgroundColor = UIColor.darkGray 
     // set different ittle for button 
     button.setTitle("Button No \(i)", for: .normal) 
     // set different tag to differentiate betwwen button 
     button.tag = i 
     // set same target for each button 
     button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside) 
     buttonArray.append(button) 
     //add all buttons to main view 
     view.addSubview(button) 

    } 
    // we make first button of different color so that it can act as radio button 
    let firstBtn = buttonArray[0] 
    firstBtn.backgroundColor = UIColor.green 
} 


@objc func buttonTapped(_ tappedBtn : UIButton){ 
    // here we get tapped button so from array of button we match the selected one using tag and make it of different color all all other buttons of some different color 
    for button in buttonArray { 
     if(button.tag == tappedBtn.tag){ 
      button.backgroundColor = UIColor.green 

     }else{ 
      button.backgroundColor = UIColor.darkGray 
     } 
    } 

} 
を保存するためのボタン配列を作成するあなたの条件 (SWIFT 4)

//に応じて変更しています

関連する問題