Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import SwiftUI
struct Model {
var title: String
var body: String
}
struct ContentView : View {
@State var models : [Model] = [
Model(title: "First Entry", body: "first body"),
Model(title: "Second Entry", body: "second body"),
Model(title: "Third Entry", body: "third body")
]
var body: some View {
NavigationView() {
List{
ForEach(0..<models.count){ index in
NavigationLink(destination: ModelDetail(model: self.$models[index])) {
HStack {
Text(self.models[index].title)
}
}
}
}
.navigationBarTitle("Models")
.navigationBarItems(trailing: EditButton())
}
}
}
struct ModelDetail: View {
@Binding var model: Model
var body: some View {
VStack {
Text(model.title)
Text(model.body)
Button(action: {
self.model.title = "My changed title from subview"
}) {
Text("Change the title")
}
}
}
}
struct View_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment