Last active
February 27, 2021 22:45
-
-
Save DaisukeNagata/40fa8807833c1206bfd8d9dd77ffb219 to your computer and use it in GitHub Desktop.
SwiftUI_List_Segue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) } | |
} | |
} | |
} |
Author
DaisukeNagata
commented
Oct 23, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment