Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active February 27, 2021 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/40fa8807833c1206bfd8d9dd77ffb219 to your computer and use it in GitHub Desktop.
Save DaisukeNagata/40fa8807833c1206bfd8d9dd77ffb219 to your computer and use it in GitHub Desktop.
SwiftUI_List_Segue
import SwiftUI
final class TextModel: ObservableObject {
@Published var roomIndex = Array<String>()
}
struct ContentView: View {
@State private var tag : Int?
@State private var view: AnyView?
@State private var room = ""
@State private var c: ContentView2?
@ObservedObject var textModel = TextModel()
var body: some View {
NavigationView {
VStack {
TextField("Room", text: $room,
onCommit: {
textModel.roomIndex.append(room)
self.c = ContentView2(viewModel: textModel)
self.tag = 0
})
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Spacer()
NavigationLink(destination: AnyView(c), tag: self.tag ?? 0, selection: $tag) {
EmptyView()
}
}
.navigationBarTitle("", displayMode: .inline)
.onDisappear {
room = ""
}
}
}
}
struct ContentView2: View {
@State private var tag : Int?
@State private var view: AnyView?
@State private var t: TextModel?
@ObservedObject var viewModel: TextModel
var body: some View {
List(self.viewModel.roomIndex.indices, id: \.self) { index in
NavigationLink(destination: Text (self.viewModel.roomIndex[index])) { Text(self.viewModel.roomIndex[index]) }
}
}
}
@DaisukeNagata
Copy link
Author

LISTSAMPLE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment