Calling view and go back

Hello guys,


I'm having a little problem again. After creating a new object, I would like to branch to a detaillier on the iPhone without any user action. But after adding the data there should be a manual possibility to come back. Is there a possibility for this? Unfortunately I have not found anything yet.


Thanks a lot.


The problem is shown in the code below:

import SwiftUI
  
class L: ObservableObject {
 @Published var x = ["a", "b", "c"]
}

struct ContentView: View {
 @ObservedObject var l = L()
    
 var body: some View {
  NavigationView {
   List {
    ForEach (l.x, id: \.self) {x in
     NavigationLink(destination: sv()) {
      Text(x)
     }.navigationBarTitle(Text("List"))
      .navigationBarItems(trailing: Button(action: self.addnewvalue, label: { Text("New")}))
    }
   }
  }
 }
 func addnewvalue() {
  l.x.append("d")
  //how to get automatically into the detailview sv?
 }
}
  
  
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
  
struct sv: View {
    var body: some View {
        Text("Hello, World!")
    //how to get back with buttom into the main view?
    }
}

After some struggling, I made it work with this (not completely clean, but shows principle


import SwiftUI

class L: ObservableObject {
@Published var x = ["a", "b", "c"]
}
 
struct ContentView: View {
@ObservedObject var l = L()
    @State var autoLink = [false, false, false]
      @State var destSV = "a"
    @State private var itemsCount = 3

var body: some View {
  NavigationView {
   List {
//    ForEach (l.x, id: \.self) {x in  // Use index to navigate in several arrays
    ForEach (0 ..< itemsCount, id: \.self) {index in
        NavigationLink(destination: sv(to: self.$destSV), isActive: self.$autoLink[index]) {
            Text(self.l.x[index])
     }.navigationBarTitle(Text("List"))
      .navigationBarItems(trailing: Button(action: self.addnewvalue, label: { Text("New")}))
    }
   }
  }
}
   
func addnewvalue() {
    let newName = String(Character(UnicodeScalar(97 + itemsCount) ?? "a"))  // hardcoded 'a' as 97
//    l.x.append("d")
    //how to get automatically into the detailview sv?
    l.x.append(newName)
    itemsCount += 1
    autoLink.append(true)
    destSV = newName
}
   
}
   
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
   
struct sv: View {
    @Binding var to: String
    var body: some View {
        Text("Hello, World! \(to)")
         //how to get back with buttom into the main view?
         // NOW, you return
    }
}

Hello, Claude,


Thank you very much. I see you made this whole thing a lot more flexible than I wanted. The second view is the detail view and is always the same for this species.


My idea was to create a new element from a list (normal object) and then automatically go to the detail view instead of letting the user press a dummy entry.


Your solution is simply ingenious.


I will have a closer look at it in the next few hours.

In fact, you always go to the same SV, just the content is adapted.


I noticed an error in my code.

The label is always the same whatever item is clicked.

Should replace

                    NavigationLink(destination: sv(to: self.$destSV), isActive: self.$autoLink[index]) {
                        Text(self.l.x[index])

with

                    NavigationLink(destination: sv(to: self.$l.x[index]), isActive: self.$autoLink[index]) {
                        Text(self.l.x[index])


As a consequence, destSV is no more needed.

Can comment it out on lines 10 and 34.

Wish you good continuation.

Calling view and go back
 
 
Q