0

enter image description here

I have a number of buttons that all have the same action so I’ve used a collection View

@IBOutlet var playerButtons: [UIButton]!

I was hoping that using a picker view would allow me to select a name and then set the title of the button, however I cant seem to find out how to achieve this in a collection view.

The function I’m trying to use docent seem to work though

open func setTitle(_ title: String?, for state: UIControlState)

My full code is

import UIKit
import Firebase


class TeamSelection: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    //IBOutlets Collection for Player Names
    @IBOutlet var playerButtons: [UIButton]!
    @IBOutlet var Player: [UIView]!
    @IBOutlet weak var homeTeamLabel: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!

    var player = [Players]()
    var club: Clubs!
    var clubName = String()
    var playerFirstName = String()
    var playerLastName = String()

    // Will be removed and Firebase used in place
    var players = ["player 1", "player 2", "player 3", "and so on"]

    override func viewDidLoad() {
        super.viewDidLoad()

        pickerView.dataSource = self
        pickerView.delegate = self

        CLUB_KEY = ""
        CLUB_KEY = club.clubKey

        self.navigationItem.title = club.clubName

        DataService.ds.REF_PLAYERS.queryOrdered(byChild: "clubKey").queryEqual(toValue: club.clubKey).observe(.value, with: { (snapshot) in

            print("PLAYERS_COUNT: \(snapshot.childrenCount)")
            print("PLAYERS_SNAPSHOT: \(snapshot)")

            self.player = []
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {

                for snap in snapshots {
                    if let playerDict = snap.value as? Dictionary<String, AnyObject> {
                        let key = snap.key

                        let players = Players(playerKey: key, dictionary: playerDict)
                        self.player.append(players)

                    }
                }
            }
//            self.tableView.reloadData()
        }) { (error) in
            print(error.localizedDescription)
            print("CHET: local error")
        }
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {

        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return players.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return players[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

//        self.pickerView.isHidden = true

        // Missing command
           playerButtons.setTitle
    }

    //IBActions Collection for Player Names
    @IBAction func playerSelectionAction(_ sender: UIButton) {

        sender.setTitle("buttonName", for: .normal)

        let sender = sender.tag
        print(sender)


    }    
}
Chet
  • 53
  • 1
  • 13
  • Possible duplicate of [Changing text of UIButton programmatically swift](https://stackoverflow.com/questions/26326296/changing-text-of-uibutton-programmatically-swift) – mfaani May 29 '17 at 18:55
  • Following the duplicate link: make your changes in the `viewDidLoad` – mfaani May 29 '17 at 18:56
  • I don't have the .setTitle available to me – Chet May 29 '17 at 19:02
  • That's impossible. For every UIButton instance that function exists. You already have it in your question as `sender.setTitle("buttonName", for: .normal)` But you must do that in your `viewDidLoad`. You shouldn't do it in the `playerSelectionAction` function....because it would run that **after** you click the button. But you the title to change **before** your viewController comes up...so place that line in your `viewDidLoad`... – mfaani May 29 '17 at 19:15
  • Value of type '[UIButton]' has no member 'setTitle' but if I use a single outlet, not a collection it works fine @IBOutlet weak var singlePlayerButton: UIButton! – Chet May 29 '17 at 20:02
  • I'm not sure if I follow you. See if [this moment](https://youtu.be/_lRx1zoriPo?t=2676) of the video helpful – mfaani May 29 '17 at 21:02

1 Answers1

0

Sorted with the following

@IBAction func playerSelectionAction(_ sender: UIButton) {

        let senderINT = sender.tag

        let selectedBtn = self.view.viewWithTag(senderINT) as! UIButton

        selectedBtn.setTitle("", for: .normal)

        print(sender)

    }
Chet
  • 53
  • 1
  • 13