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