Tuesday, October 20, 2026
HomeiOS Developmentios - Name UIButton from String Programmatically

ios – Name UIButton from String Programmatically

[ad_1]

That is my code for my CollectionViews. I’ve two CollectionViews thus far, and as you’ll be able to see within the Strings within the code, they every have 6 buttons. I must name the buttons from the Strings in order that when they’re pressed, a brand new window opens. The brand new window will comprise issues like flashcards, pertaining to the topic of the button that was pressed.

For reference, right here is what the simulator seems to be like.

code:

     import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
tremendous.viewDidLoad()
// Do any extra setup after loading the view.
    }


}

class FirstCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var buttonOne: UIButton!

override func awakeFromNib() {
tremendous.awakeFromNib()
commonInit()
    }
    func commonInit() {
guard buttonOne != nil else { return }
buttonOne.titleLabel!.font = UIFont(identify: "Marker Felt", measurement: 20)
buttonOne.layer.cornerRadius = 10
buttonOne.clipsToBounds = true
buttonOne.layer.borderWidth = 1.0
buttonOne.layer.borderColor = UIColor.white.cgColor
}
    }

class SecondCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var buttonTwo: UIButton!
    override func awakeFromNib() {
tremendous.awakeFromNib()
commonInit()
    }
    func commonInit() {
guard buttonTwo != nil else { return }
buttonTwo.titleLabel!.font = UIFont(identify: "Marker Felt", measurement: 20)
buttonTwo.layer.cornerRadius = 10
buttonTwo.clipsToBounds = true
buttonTwo.layer.borderWidth = 1.0
buttonTwo.layer.borderColor = UIColor.white.cgColor
}
}

class TwoCollectionsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var firstCV: UICollectionView!
    @IBOutlet weak var secondCV: UICollectionView!
    let firstData: [String] = [
"Good Work", "Nice Try", "Btn 3", "Btn 4", "Btn 5", "Btn 6"]
    let secondData: [String] = [
"Second 1", "Second 2", "Second 3", "Second 4", "Second 5", "Second 6"]

    override func viewDidLoad() {
tremendous.viewDidLoad()

firstCV.dataSource = self
firstCV.delegate = self

secondCV.dataSource = self
secondCV.delegate = self
   }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {


if collectionView == firstCV {
    return firstData.rely
}


return secondData.rely
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


if collectionView == firstCV {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "firstCell", for: indexPath) as! FirstCollectionViewCell
    cell.buttonOne.setTitle(firstData[indexPath.item], for: [])
    return cell
    
    
}


let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "secondCell", for: indexPath) as! SecondCollectionViewCell
cell.buttonTwo.setTitle(secondData[indexPath.item], for: [])
return cell

    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if collectionView == firstCV {
    print("Chosen Merchandise: (indexPath.merchandise) in First CV")
   
    return
}

print("Chosen Merchandise: (indexPath.merchandise) in Second CV")



    }

}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments