Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active July 13, 2020 09:54
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 chriseidhof/3de9160dd561bbce9dc8ab0b222d5e9c to your computer and use it in GitHub Desktop.
Save chriseidhof/3de9160dd561bbce9dc8ab0b222d5e9c to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// AddressBook
//
// Created by Chris Eidhof on 13.07.20.
//
import SwiftUI
final class Contact: ObservableObject, Identifiable {
let id = UUID()
@Published var name: String
@Published var city: String
init(name: String, city: String) {
self.name = name
self.city = city
}
}
var contacts: [Contact] = [
Contact(name: "John Appleseed", city: "Cupertino"),
Contact(name: "Kate Bell", city: "San Francisco")
]
struct PersonCell: View {
@Binding var selectedPerson: Contact?
@ObservedObject var person: Contact
var body: some View {
Button(person.name) {
selectedPerson = person
}
}
}
struct PersonDetail: View {
@StateObject var person: Contact
var body: some View {
return VStack {
Text(person.name).bold()
Text(person.city)
}
}
}
struct ContentView: View {
@State var selectedPerson: Contact?
var body: some View {
NavigationView {
List(contacts) { person in
PersonCell(selectedPerson: $selectedPerson, person: person)
}.navigationTitle("People")
if let p = selectedPerson {
PersonDetail(person: p)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// ContentView.swift
// AddressBook
//
// Created by Chris Eidhof on 13.07.20.
//
import SwiftUI
final class Person: ObservableObject, Identifiable {
let id = UUID()
@Published var name: String
@Published var city: String
init(name: String, city: String) {
self.name = name
self.city = city
}
}
var people: [Person] = [
Person(name: "John Appleseed", city: "Cupertino"),
Person(name: "Kate Bell", city: "San Francisco")
]
struct PersonCell: View {
@Binding var selectedPersonId: UUID
@ObservedObject var person: Person
var body: some View {
Button(person.name) {
selectedPersonId = self.person.id
}
}
}
struct PersonDetail: View {
@StateObject var person: Person
init(id: UUID) {
let p = people.first { $0.id == id }!
print(p.name)
self._person = StateObject(wrappedValue: p)
}
var body: some View {
return VStack {
Text(person.name).bold()
Text(person.city)
}
}
}
struct ContentView: View {
@State var selectedPersonId = people[0].id
var body: some View {
NavigationView {
List(people) { person in
PersonCell(selectedPersonId: $selectedPersonId, person: person)
}.navigationTitle("People")
PersonDetail(id: selectedPersonId)
// to make this work, either add `.id(selectedPersonId)` to the `PersonDetail` view, or use the solution below.
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// ContentView.swift
// AddressBook
//
// Created by Chris Eidhof on 13.07.20.
//
import SwiftUI
final class Person: ObservableObject, Identifiable {
let id = UUID()
@Published var name: String
@Published var city: String
init(name: String, city: String) {
self.name = name
self.city = city
}
}
var people: [Person] = [
Person(name: "John Appleseed", city: "Cupertino"),
Person(name: "Kate Bell", city: "San Francisco")
]
struct PersonCell: View {
@StateObject var person: Person
var body: some View {
NavigationLink(person.name, destination: PersonDetail(person: person))
}
}
struct PersonDetail: View {
@StateObject var person: Person
var body: some View {
return VStack {
Text(person.name).bold()
Text(person.city)
}
}
}
struct ContentView: View {
var body: some View {
NavigationView {
List(people) { person in
PersonCell(person: person)
}.navigationTitle("People")
Text("Select a person")
}
}
}
struct ContentView_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