iOS) UIButton tag: forEach문 index로 지정하기

minin·2022년 3월 29일
0

Swift연습장

목록 보기
10/11

enumerated()를 이용하면 각 요소의 index를 이용할 수 있다.
버튼들을 []araay에 담아두고, enumerated()를 통해 index, item을 사용한다.
이 때, 버튼 자체에 대한 속성을 설정할 때에는 item을, index를 사용할 때에는 index를 이용한다.

아래와 같이 item.tag = index를 사용하면, 한 번에 태그와 속성을 정해줄 수 있다.

[tasteStar00, tasteStar01, tasteStar02, tasteStar03, tasteStar04].enumerated().forEach { (index, item) in
	item.tag = index
	item.setImage(UIImage(systemName: "star"), for: .normal)
	item.tintColor = .yellow
}

🔖 참고

profile
🍫 iOS 🍫 Swift

0개의 댓글