3

I'm beginner of SwiftUI

I recently learning SwiftUI with Youtube Lecture

In TabView part,

TabView {
     myView(titleText: "프로필", bgColor: Color.blue) // it's just View I made
                 .tabItem { 
                     Image(systemName : "person.crop.circle.fill")
                     Text("프로필")
                 }.tag(2) // <- I'm wondering about this 'tag'
}

I'm wondering what does 'tag(_)' do in tabItem

thank you for your answer

999rimmy
  • 35
  • 3

1 Answers1

2

It is for selection tracking

@State private var selected: Int = 0   // by default `first` tab, or any you want

TabView(selection: $selected) {
     myView(titleText: "프로필", bgColor: Color.blue) // it's just View I made
                 .tabItem { 
                     Image(systemName : "person.crop.circle.fill")
                     Text("프로필")
                 }.tag(2) // <- I'm wondering about this 'tag'
}

so you can track when selected is modified, say by onChange(of: or change selection programmatically in any action, say

Button("프로필") { selected = 2 }
Asperi
  • 228,894
  • 20
  • 464
  • 690