Skip to content

Instantly share code, notes, and snippets.

@eivindml
Last active August 28, 2019 08:14
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 eivindml/10b5e1354e5cf68c824fc8f6440131a3 to your computer and use it in GitHub Desktop.
Save eivindml/10b5e1354e5cf68c824fc8f6440131a3 to your computer and use it in GitHub Desktop.
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