HomeiOS Developmentios - (2023) NavigationStack and NavigationDestination, utilizing NavigationPath Appropriately

ios – (2023) NavigationStack and NavigationDestination, utilizing NavigationPath Appropriately


I’m attempting to perform a comparatively easy activity. I wish to navigate from a root view -> intermediate view that captures enter -> ultimate view that shows enter -> again to root view.

The answer I got here up with makes use of the brand new NavigationPath and NavigationStack constructs and is partially derived from this stack overflow publish: A posh Navigation with NavigationPath in SwiftUI

I created an enum with my views (NAV_ITEMS), then created a path of kind NAV_ITEMS, and pushed onto that path after I needed to go to the subsequent view. A downfall of this resolution is that I handle the state variable, known as newRoomName, from the foundation view and move it down as a binding to each little one views, which appears incorrect.

import SwiftUI

enum NAV_VIEWS {
    case AddRoomName, AddRoomSuccess, addHomeName, addHomeNameSuccess
}

struct NavPathTestView: View {
    
    @State var presentedViews = [NAV_VIEWS]()
    @State var newRoomName: String = ""

        var physique: some View {
            NavigationStack(path: $presentedViews) {
                NavigationLink(worth: NAV_VIEWS.AddRoomName) {
                    HStack {
                        Textual content("Add Room")
                    }.padding()
                        .foregroundColor(.white)
                        .background(Colour.crimson)
                        .cornerRadius(40)
                }

                .navigationDestination(for: NAV_VIEWS.self) { i in
                    swap i {
                    case .AddRoomName:
                            RoomView(presentedViews: $presentedViews, newRoomName: $newRoomName)
                    case .AddRoomSuccess:
                            RoomSuccessView1(presentedViews: $presentedViews, newRoomName: $newRoomName)
                    case .addHomeName:
                        Textual content("add House Title")
                    case .addHomeNameSuccess:
                        Textual content("Add House Success")
                    }
                }
                .navigationTitle("Root Web page")
            }
        }
}

struct RoomView: View {
      
    @Binding var presentedViews : [NAV_VIEWS]
    @Binding var newRoomName: String
    
    var physique: some View {
        
        VStack() {
            
            Textual content("Add Room Title")
            Spacer()
            TextField("My new Room", textual content: $newRoomName)
            .padding()
            .textInputAutocapitalization(.phrases)
            .cornerRadius(20)
            .onSubmit {
                print("NewRoomName: (newRoomName)")
            }
            Spacer()
            Button (motion: {
                presentedViews.append(NAV_VIEWS.AddRoomSuccess)
            })
            {
                HStack {
                    Textual content("Go to Success")
                }.padding()
                    .foregroundColor(.white)
                    .background(Colour.crimson)
                    .cornerRadius(40)
            }
        }
        
    }
}

struct RoomSuccessView1: View {
    
    @Binding var presentedViews : [NAV_VIEWS]
    @Binding var newRoomName: String
    
    var physique: some View {
        
        
        VStack {
            Textual content("Room Success Web page")
            Spacer()
            Textual content("The handed newRoomName: (newRoomName)")
            Spacer()
            Button (motion: {
                presentedViews.removeLast(presentedViews.rely)
            })
            {
                HStack {
                    Textual content("Again To Root")
                }.padding()
                    .foregroundColor(.white)
                    .background(Colour.crimson)
                    .cornerRadius(40)
            }
        }
    }
}


struct NavPathTestView_Previews: PreviewProvider {
    static var previews: some View {
        NavPathTestView()
    }
}

Is there a greater method to do that? Routing utilizing navigationDestination on every little one view prompted path points and animation points with how screens got here into the presentation.

For instance, if I used a navigationDestination on every little one view (child1 -> child2) then known as RootView() from the child2 view to navigate again to root, it could return, however the subsequent time that child1 was opened it could slide in from the left hand aspect of the display, totally different than the conventional slide in from the suitable.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments