OS ʹΑΔมԽ͕ܹ͍͠ Navigation • Fire and forget / State driven Navigation API • SwiftUI ͷ Navigation API ʹ͓͚Δ՝ • Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ • ʮswiftui-navigationʯͷΞϓϩʔν • ·ͱΊ 3
park.name, // ભҠݩͷը໘ʹදࣔ͢Δ title value: park // ҙͷܕͷ value ) } .navigationDestination(for: Park.self) { park in ParkDetails(park: park) } } SwiftUI Navigation ͷ၆ᛌ > OS ʹΑΔมԽ͕ܹ͍͠ Navigation
{ note in NavigationLink( note.title, destination: NoteEditor(id: note.id) ) } Text("Select a Note") } SwiftUI Navigation ͷ၆ᛌ > Fire and forget / State driven Navigation API
• Fire and forget API ΑΓෳࡶͰ͋Δ͕ɺϓϩάϥϜతͳ੍ޚ͕ ՄೳͱͳΔͨΊɺ͍ํʹΑͬͯ Deep Link ͳͲ࣮ݱͰ͖Δ 43 SwiftUI Navigation ͷ၆ᛌ > Fire and forget / State driven Navigation API
ͷঢ়ଶΛ࣋ͨͤΔͷඍົ // ͔͠ ContentView ͷ selectedTab @State ͳͷͰޡΓ let selectedTab: Tab var body: some View { ContentView(selectedTab: selectedTab) } } SwiftUI Navigation ͷ၆ᛌ > Fire and forget / State driven Navigation API
var selectedTab: Tab init(selectedTab: Tab = .received) { self.selectedTab = selectedTab } } SwiftUI Navigation ͷ၆ᛌ > Fire and forget / State driven Navigation API
var draft: Post? var body: some View { Button("Edit") { draft = Post() } .sheet(item: $draft) { (draft: Post) in EditPostView(post: draft) } } } struct EditPostView: View { let post: Post var body: some View { ... } } SwiftUI ͷ Navigation API ʹ͓͚Δ՝ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍
var draft: Post? var body: some View { Button("Edit") { draft = Post() } .sheet(item: $draft) { (draft: Post) in EditPostView(post: draft) } } } struct EditPostView: View { let post: Post var body: some View { ... } } SwiftUI ͷ Navigation API ʹ͓͚Δ՝ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍
Λ͢͜ͱ͕Ͱ͖Δ API overloads 2. ঢ়ଶཧΛෳࡶʹͤ͞Δ API ͕ଘࡏ͍ͯ͠Δ • ࠷ݶͷঢ়ଶͷΈΛཁٻ͢Δ API overloads 3. ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ • Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔
Λ͢͜ͱ͕Ͱ͖Δ API overloads 2. ঢ়ଶཧΛෳࡶʹͤ͞Δ API ͕ଘࡏ͍ͯ͠Δ • ࠷ݶͷঢ়ଶͷΈΛཁٻ͢Δ API overloads 3. ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ • Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔
Λ͢͜ͱ͕Ͱ͖Δ API overloads 2. ঢ়ଶཧΛෳࡶʹͤ͞Δ API ͕ଘࡏ͍ͯ͠Δ • ࠷ݶͷঢ়ଶͷΈΛཁٻ͢Δ API overloads 3. ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ • Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍ > Binding value Λ͢͜ͱ͕Ͱ͖Δ API overloads
var draft: Post? var body: some View { Button("Edit") { draft = Post() } .sheet(item: $draft) { (draft: Post) in EditPostView(post: draft) } } } struct EditPostView: View { let post: Post var body: some View { ... } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍ > Binding value Λ͢͜ͱ͕Ͱ͖Δ API overloads
@State var draft: Post? var body: some View { Button("Edit") { draft = Post() } .sheet(item: $draft) { (draft: Post) in EditPostView(post: draft) } } } struct EditPostView: View { let post: Post var body: some View { ... } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍ > Binding value Λ͢͜ͱ͕Ͱ͖Δ API overloads
@State var draft: Post? var body: some View { Button("Edit") { draft = Post() } .sheet(item: $draft) { ($draft: Post) in // closure ʹ Binding value ΛͤΔΑ͏ʹͳͬͨΒྑͦ͞͏ EditPostView(post: $draft) } } } struct EditPostView: View { @Binding var post: Post var body: some View { ... } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍ > Binding value Λ͢͜ͱ͕Ͱ͖Δ API overloads
optionalValue: Binding<Value?>, @ViewBuilder content: @escaping (Binding<Value>) -> Content ) -> some View where Value: Identifiable, Content: View { self.sheet(item: optionalValue) { _ in if let value = Binding(unwrapping: optionalValue) { content(value) } } } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍ > Binding value Λ͢͜ͱ͕Ͱ͖Δ API overloads
optionalValue: Binding<Value?>, @ViewBuilder content: @escaping (Binding<Value>) -> Content ) -> some View where Value: Identifiable, Content: View { self.sheet(item: optionalValue) { _ in if let value = Binding(unwrapping: optionalValue) { content(value) } } } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ભҠઌͷը໘ʹ Binding value Λ͢ػߏΛඋ͍͑ͯͳ͍ > Binding value Λ͢͜ͱ͕Ͱ͖Δ API overloads
Λ͢͜ͱ͕Ͱ͖Δ API overloads 2. ঢ়ଶཧΛෳࡶʹͤ͞Δ API ͕ଘࡏ͍ͯ͠Δ • ࠷ݶͷঢ়ଶͷΈΛཁٻ͢Δ API overloads 3. ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ • Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ঢ়ଶཧΛෳࡶʹͤ͞Δ API ͕ଘࡏ͍ͯ͠Δ > ࠷ݶͷঢ়ଶͷΈΛཁٻ͢Δ API overloads
draft: Post? @State var settings: Settings? @State var userProfile: Profile? " enum Route { case draft(Post) case settings(Settings) case userProfile(Profile) } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ > Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads
var route: Route? // ... .sheet( item: Binding<Post?>( get: { if case let .draft(post) = route return post } else { return nil } }, set: { post if let post = post { route = .draft(post) } } ) ) { post in /* ... */ } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ > Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads
var route: Route? // ... .sheet( item: Binding<Post?>( get: { if case let .draft(post) = route return post } else { return nil } }, set: { post if let post = post { route = .draft(post) } } ) ) { post in /* ... */ } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ > Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads
var route: Route? // ... .sheet( item: Binding<Post?>( get: { if case let .draft(post) = route return post } else { return nil } }, set: { post if let post = post { route = .draft(post) } } ) ) { post in /* ... */ } } Navigation API ͷ՝ʹͲ͏ཱ͔ͪ͏͔ > ෳͷ Navigation Λཧͩ͢͠ͱແବͳঢ়ଶ͕૿͑Δ > Navigation Λ enum ͰཧͰ͖ΔΑ͏ʹ͢Δ API overloads