タブバーを作成する
Building our tab bar
// // MeView.swift // HotProspects // import SwiftUI struct MeView: View { var body: some View { Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) } }
// // ProspectsView.swift // HotProspects import SwiftUI struct ProspectsView: View { enum FilterType { case none, contacted, uncontacted } let filter: FilterType var title: String { switch filter { case .none: return "Everyone" case .contacted: return "Contacted people" case .uncontacted: return "Uncontacted people" } } var body: some View { NavigationView { Text("Hello, World!") .navigationBarTitle(title) } } }
// // ContentView.swift // HotProspects // // Created by Naoki Abe on 2020/08/29. // Copyright © 2020 Naoki Abe. All rights reserved. // import SwiftUI struct ContentView: View { var body: some View { TabView { ProspectsView(filter: .none) .tabItem { Image(systemName: "person.3") Text("Everyone") } ProspectsView(filter: .contacted) .tabItem { Image(systemName: "checkmark.circle") Text("Contacted") } ProspectsView(filter: .uncontacted) .tabItem { Image(systemName: "questionmark.diamond") Text("Uncontacted") } MeView() .tabItem { Image(systemName: "person.crop.square") Text("Me") } } } }
@EnvironmentObjectを使用してタブ間でデータを共有する
Sharing data across tabs using @EnvironmentObject
複数のビュー間でそれを共有することになると、SwiftUIの環境の最も優れている点の1つはObservableObject、@ObservedObjectプロパティラッパーで使用していたのと同じプロトコルを使用することです。これは、@Publishedプロパティラッパーを使用してアナウンスする必要のあるプロパティをマークできることを意味します。
プロパティをSwiftUI環境にポストして、すべての子ビューがアクセスできるようにする必要があります。タブは内部にあるタブビューの子と見なされるため、タブを環境に追加すると、TabViewすべてのProspectsViewインスタンスがそのオブジェクトを取得します。
したがって、この修飾子をTabViewinに追加しContentViewます。
import SwiftUI class Prospect: Identifiable, Codable { let id = UUID() var name = "Anonymous" var emailAddress = "" var isContacted = false } class Prospects: ObservableObject { @Published var people: [Prospect] init() { self.people = [] } }
// // ProspectsView.swift // HotProspects import SwiftUI struct ProspectsView: View { @EnvironmentObject var prospects: Prospects enum FilterType { case none, contacted, uncontacted } let filter: FilterType var title: String { switch filter { case .none: return "Everyone" case .contacted: return "Contacted people" case .uncontacted: return "Uncontacted people" } } var body: some View { NavigationView { Text("People: \(prospects.people.count)") .navigationBarTitle(title) .navigationBarItems(trailing: Button(action: { let prospect = Prospect() prospect.name = "Paul Hudson" prospect.emailAddress = "paul@hackingwithswift.com" self.prospects.people.append(prospect) }) { Image(systemName: "qrcode.viewfinder") Text("Scan") }) } } }
import SwiftUI struct ContentView: View { var prospects = Prospects() var body: some View { TabView { ProspectsView(filter: .none) .tabItem { Image(systemName: "person.3") Text("Everyone") } ProspectsView(filter: .contacted) .tabItem { Image(systemName: "checkmark.circle") Text("Contacted") } ProspectsView(filter: .uncontacted) .tabItem { Image(systemName: "questionmark.diamond") Text("Uncontacted") } MeView() .tabItem { Image(systemName: "person.crop.square") Text("Me") } } .environmentObject(prospects) } }
SwiftUIリストの動的フィルタリング
Dynamically filtering a SwiftUI List
// // ProspectsView.swift // HotProspects // // Created by Naoki Abe on 2020/09/01. // Copyright © 2020 Naoki Abe. All rights reserved. // import SwiftUI struct ProspectsView: View { @EnvironmentObject var prospects: Prospects enum FilterType { case none, contacted, uncontacted } let filter: FilterType var title: String { switch filter { case .none: return "Everyone" case .contacted: return "Contacted people" case .uncontacted: return "Uncontacted people" } } var filteredProspects: [Prospect] { switch filter { case .none: return prospects.people case .contacted: return prospects.people.filter { $0.isContacted } case .uncontacted: return prospects.people.filter { !$0.isContacted } } } var body: some View { NavigationView { // Text("People: \(prospects.people.count)") List { ForEach(filteredProspects) { prospect in VStack(alignment: .leading) { Text(prospect.name) .font(.headline) Text(prospect.emailAddress) .foregroundColor(.secondary) } } } .navigationBarTitle(title) .navigationBarItems(trailing: Button(action: { let prospect = Prospect() prospect.name = "Paul Hudson" prospect.emailAddress = "paul@hackingwithswift.com" self.prospects.people.append(prospect) }) { Image(systemName: "qrcode.viewfinder") Text("Scan") }) } } }