2
votes

I'm testing out SwiftUI by building an app that has a "Settings-View", let's call it ViewB. This view has a list where you can select a language. The first view, ViewA has 2 buttons "Open" or "Select language".

What I want to do is, starting from ViewA, open ViewB, select a language and automatically come back to ViewA. This is what I've done:

ViewA

var body: some View {
    NavigationView {
        VStack(alignment: .center) {
            Button(action: start) {
                Text("Open")
            }
            NavigationLink(destination: ViewB()) {
                Text("Change language")
            }
        }
        .sheet(isPresented: $presenting) {
            DemoView(code: self.langugage ?? "SE") { result in }
        }
    }
}

And this is how far I've come on ViewB:

var body: some View {
    List(langs) { item in
        HStack {
            NavigationLink(destination: ViewA(presentingSuccessScreen: self.$isPresentingSuccessScreen, language: item.code)) {
                Image(item.icon).resizable().frame(width: 40, height: 40)
                Text(item.market)
            }.frame(height: 64)
        }
        .navigationBarTitle(Text("Select Language"), displayMode: .inline)
        .navigationBarBackButtonHidden(true)
        .navigationBarItems(trailing:
            Button("Cancel") {
                self.presentationMode.wrappedValue.dismiss()
        })
    }
}

I would like to have the ">" image on the right side of each row, which you get automatically with NavigationList and I want the whole row to be selectable. But by doing it this way, all I get is the destionation view pushing on top of the stack, so If I change language multiple times, I end up with a lot of views on top of each other.

Any help will be appreciated!

1

1 Answers

2
votes

NavigationLink works only in one direction. Instead you need something like

HStack {
    VStack {
        Image(item.icon).resizable().frame(width: 40, height: 40)
        Text(item.market)
    }.frame(height: 64)
    .onTapGesture {
        // set item.code & success flag to some view model and then
        // just call dismiss to return to ViewA
        self.presentationMode.wrappedValue.dismiss()
    }
}