value being constant in swiftUI - ios

I am sorry im not sure that the title makes sense but if u read, im sure u will understand my problem. I have declared a variable #State var timetext: Int32 in the file CreatingWorkout, with a textfield TextField("5000, 100 etc", value: $timetext, formatter: NumberFormatter()) When i go to the createWorkoutView file and try to present it with the sheet, it wants me to give a value to timetext. However, when i provide a value with the textfield it stays constantly value given when calling with the sheet. I will attach a video here for you to see.
CreatingWorkout.swift :
struct CreatingWorkout: View {
#State var workoutTitle: String
#State var desc: String
#State var timetext: Int32
#State private var iconColor = Color.black
#State var displayWorkout: String = ""
#Environment(\.dismiss) var dismiss
#Environment(\.managedObjectContext) private var viewContext
private func saveWorkout() {
do {
let workout = Workout(context: viewContext)
workout.title = workoutTitle
workout.time = timetext
workout.icon = displayWorkout
workout.descriptionn = desc
try viewContext.save()
} catch {
print(error.localizedDescription)
}
}
CreateWorkout.swift :
import SwiftUI
struct CreateWorkoutView: View {
#State private var showingCreateWorkout = false
#State var timetext: Int32 = 0
var body: some View {
NavigationView {
VStack {
VStack(alignment: .center, spacing: 20) {
Text("Fitzy")
.font(.largeTitle)
.fontWeight(.bold)
Text("Create your first Workout")
.font(.title3)
.foregroundColor(.gray)
Button {
showingCreateWorkout.toggle()
} label: {
Image(systemName: "plus")
.font(.largeTitle)
.foregroundColor(.white)
.padding()
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.foregroundColor(Color("AccentColor"))
.frame(width: 50, height: 50)
)
}.sheet(isPresented: $showingCreateWorkout) {
CreatingWorkout(workoutTitle: "", desc: "", timetext: timetext)
}
}
}.navigationTitle("Create Workout")
}
}
}
struct CreateWorkoutView_Previews: PreviewProvider {
static var previews: some View {
CreateWorkoutView()
}
}
https://drive.google.com/file/d/1qQDQmap5bMz9LxzibV98epHW5urqUtZ7/view?usp=sharing

as mentioned in the comments, you need a #State property to pass the value
that you type in your TextField to the sheet with CreatingWorkout. Try something like this:
struct CreatingWorkout: View {
#State var workoutTitle: String
#State var desc: String
#State var timetext: Int32
// ....
var body: some View {
Text("\(timetext)")
}
}
struct CreateWorkout: View {
#State var showingCreateWorkout = false
#State var timetext: Int32 = 0 // <-- here
var body: some View {
VStack {
TextField("type a number", value: $timetext, format: .number).border(.red) // <-- here
Button {
showingCreateWorkout.toggle()
} label: {
Image(systemName: "plus").font(.largeTitle).foregroundColor(.red).padding()
.background(
RoundedRectangle(cornerRadius: 10, style: .continuous)
.foregroundColor(Color("AccentColor"))
.frame(width: 50, height: 50)
)
}.sheet(isPresented: $showingCreateWorkout) {
CreatingWorkout(workoutTitle: "", desc: "", timetext: timetext) // <-- here
}
}
}
}

Related

Why don't #State parameter changes cause a view update?

I am trying to follow the guidance in a WWDC video to use a #State struct to configure and present a child view. I would expect the struct to be able to present the view, however the config.show boolean value does not get updated when set by the button.
The code below has two buttons, each toggling a different boolean to show an overlay. Toggling showWelcome shows the overlay but toggling config.show does nothing. This seems to be working as intended, I just don't understand why SwiftUI behaves this way. Can someone explain why it's not functioning like I expect, and/or suggest a workaround?
https://developer.apple.com/videos/play/wwdc2020/10040/ # 5:14
struct InformationOverlayConfig {
#State var show = false
#State var title: String?
}
struct InformationOverlay: View {
#Binding var config: InformationOverlayConfig
var body: some View {
if config.title != nil {
Text(config.title!)
.padding()
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 15))
}
}
}
struct TestView: View {
#State private var configWelcome = InformationOverlayConfig(title: "Title is here")
#State private var showWelcome = false
var body: some View {
VStack {
Text("hello world")
Spacer()
Button("Toggle struct parameter", action: {
configWelcome.show.toggle()
})
Button("Toggle boolean state", action: {
showWelcome.toggle()
})
}
.overlay(
VStack {
InformationOverlay(config: $configWelcome).opacity(configWelcome.show ? 1 : 0)
InformationOverlay(config: $configWelcome).opacity(showWelcome ? 1 : 0)
})
}
You "Config" is not a View. State variables only go in Views.
Also, do not use force unwrapping for config.title. Optional binding or map are the non-redundant solutions.
Lastly, there is no need for config to be a Binding if it actually functions as a constant for a particular view.
struct InformationOverlay: View {
struct Config {
var show = false
var title: String?
}
let config: Config
var body: some View {
VStack {
if let title = config.title {
Text(title)
.padding()
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 15))
}
// or
config.title.map {
Text($0)
.padding()
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 15))
}
}
}
}
struct TestView: View {
#State private var configWelcome = InformationOverlay.Config(title: "Title is here")
#State private var showWelcome = false
var body: some View {
VStack {
Text("hello world")
Spacer()
Button("Toggle struct parameter") {
configWelcome.show.toggle()
}
Button("Toggle boolean state") {
showWelcome.toggle()
}
}
.overlay(
VStack {
InformationOverlay(config: configWelcome).opacity(configWelcome.show ? 1 : 0)
InformationOverlay(config: configWelcome).opacity(showWelcome ? 1 : 0)
}
)
}
}

SwiftUI - List that does not display

I don't know how to explain it, I think a video will be more explicit ..
https://www.youtube.com/watch?v=UyuGaNA6NCo
I only want to display the information according to my choices, and I don't know where I was wrong, I am in the problem since some hours
My code :
HOME VIEW
struct HomeView: View {
#ObservedObject var travelLibrary: TravelLibrary
#State private var isShowingSurveyCreation = false
var body: some View {
ZStack(alignment: Alignment(horizontal: .trailing, vertical: .bottom)) {
NavigationView {
ScrollView {
VStack {
ForEach(travelLibrary.testTravels) { travel in
ZStack {
RoundedRectangle(cornerRadius: 15, style: .continuous)
.fill(Color.white)
.shadow(color: /*#START_MENU_TOKEN#*/.black/*#END_MENU_TOKEN#*/, radius: 7)
TravelCellView(travel: travel)
}
}
}.padding(12)
}.navigationTitle(Text("Mes Voyages"))
}
PlusButtonView(action: {
isShowingSurveyCreation.toggle()
}).sheet(isPresented: $isShowingSurveyCreation, content: {
TravelCreationView(travelLibrary: travelLibrary)
})
.padding()
}
}
}
struct HomeView_Previews: PreviewProvider {
#StateObject static var travelLibrary = TravelLibrary()
static var previews: some View {
HomeView(travelLibrary: travelLibrary)
}
}
TRAVEL CREATION VIEW
import SwiftUI
struct TravelCreationView: View {
#ObservedObject var travelLibrary: TravelLibrary
#ObservedObject var travelConfig = NewTravelConfig()
#Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView {
ZStack {
Form {
Section {
Picker(selection: $travelConfig.index1, label: Text("Pays de destination : ")) {
ForEach(0 ..< travelConfig.nameCountry.count) {
Text(travelConfig.nameCountry[$0]).tag($0)
.foregroundColor(.black)
}
}
Picker(selection: $travelConfig.index2, label: Text("Type de voyage : ")) {
ForEach(0 ..< travelConfig.typeOfTravel.count) {
Text(travelConfig.typeOfTravel[$0]).tag($0)
.foregroundColor(.black)
}
}
}
Section {
Stepper(value: $travelConfig.day, in: 1...365) {
Text("Durée du séjour : \(travelConfig.day) jours")
}
}
}
}.navigationTitle("Formulaire")
.navigationBarItems(trailing: Button(action: {
let newTravel = Travel(config: travelConfig)
travelLibrary.testTravels.append(newTravel)
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Valider")
}))
}
}
}
struct TravelCreationView_Previews: PreviewProvider {
#StateObject static var travelLibrary = TravelLibrary()
static var previews: some View {
TravelCreationView(travelLibrary: travelLibrary)
}
}
//
// TravelCreationView.swift
// Final Travel Project Logistics
//
// Created by Sefkan on 04/05/2021.
//
import SwiftUI
struct TravelCreationView: View {
#ObservedObject var travelLibrary: TravelLibrary
#ObservedObject var travelConfig = NewTravelConfig()
#Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView {
ZStack {
Form {
Section {
Picker(selection: $travelConfig.index1, label: Text("Pays de destination : ")) {
ForEach(0 ..< travelConfig.nameCountry.count) {
Text(travelConfig.nameCountry[$0]).tag($0)
.foregroundColor(.black)
}
}
Picker(selection: $travelConfig.index2, label: Text("Type de voyage : ")) {
ForEach(0 ..< travelConfig.typeOfTravel.count) {
Text(travelConfig.typeOfTravel[$0]).tag($0)
.foregroundColor(.black)
}
}
}
Section {
Stepper(value: $travelConfig.day, in: 1...365) {
Text("Durée du séjour : \(travelConfig.day) jours")
}
}
}
}.navigationTitle("Formulaire")
.navigationBarItems(trailing: Button(action: {
let newTravel = Travel(config: travelConfig)
travelLibrary.testTravels.append(newTravel)
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Valider")
}))
}
}
}
struct TravelCreationView_Previews: PreviewProvider {
#StateObject static var travelLibrary = TravelLibrary()
static var previews: some View {
TravelCreationView(travelLibrary: travelLibrary)
}
}//
// TravelCreationView.swift
// Final Travel Project Logistics
//
// Created by Sefkan on 04/05/2021.
//
import SwiftUI
struct TravelCreationView: View {
#ObservedObject var travelLibrary: TravelLibrary
#ObservedObject var travelConfig = NewTravelConfig()
#Environment(\.presentationMode) var presentationMode
var body: some View {
NavigationView {
ZStack {
Form {
Section {
Picker(selection: $travelConfig.index1, label: Text("Pays de destination : ")) {
ForEach(0 ..< travelConfig.nameCountry.count) {
Text(travelConfig.nameCountry[$0]).tag($0)
.foregroundColor(.black)
}
}
Picker(selection: $travelConfig.index2, label: Text("Type de voyage : ")) {
ForEach(0 ..< travelConfig.typeOfTravel.count) {
Text(travelConfig.typeOfTravel[$0]).tag($0)
.foregroundColor(.black)
}
}
}
Section {
Stepper(value: $travelConfig.day, in: 1...365) {
Text("Durée du séjour : \(travelConfig.day) jours")
}
}
}
}.navigationTitle("Formulaire")
.navigationBarItems(trailing: Button(action: {
let newTravel = Travel(config: travelConfig)
travelLibrary.testTravels.append(newTravel)
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Valider")
}))
}
}
}
struct TravelCreationView_Previews: PreviewProvider {
#StateObject static var travelLibrary = TravelLibrary()
static var previews: some View {
TravelCreationView(travelLibrary: travelLibrary)
}
}
TRAVELCELLVIEW
//
// TravelCellView.swift
// Final Travel Project Logistics
//
// Created by Sefkan on 04/05/2021.
//
import SwiftUI
struct TravelCellView: View {
let travel : Travel
var body: some View {
HStack {
Image(travel.flagCountry)
.resizable()
.aspectRatio(contentMode: /*#START_MENU_TOKEN#*/.fill/*#END_MENU_TOKEN#*/)
.frame(width: 80, height: 80)
.clipShape(/*#START_MENU_TOKEN#*/Circle()/*#END_MENU_TOKEN#*/)
.overlay(Circle().stroke(Color.white, lineWidth: 4))
.shadow(radius: 10)
.padding(.trailing, 8)
VStack(alignment : .leading) {
Text(travel.nameCountry)
.font(.title3)
.fontWeight(.bold)
.foregroundColor(Color.black)
.padding(.bottom, -4)
Text("Durée du voyage: \(travel.day) jours")
.font(.subheadline)
.fontWeight(.semibold)
.foregroundColor(Color.black)
.padding(.bottom, -4)
Text(travel.typeOfTravel)
.font(.subheadline)
.foregroundColor(Color.black)
}
Spacer()
FavouriteButtonView()
}.padding()
}
}
struct TravelCellView_Previews: PreviewProvider {
private static let testTravel = Travel(flagCountry: "USAflag", nameCountry: "États-Unis", typeOfTravel: "Tourisme", day: 0, isFavourite: false)
static var previews: some View {
TravelCellView(travel: testTravel)
.previewLayout(.sizeThatFits)
}
}
DATA VIEW
import Foundation
struct Travel: Identifiable {
let id = UUID().uuidString
let flagCountry: String
let nameCountry: String
let typeOfTravel: String
let day: Int
var isFavourite: Bool
init(flagCountry: String, nameCountry: String, typeOfTravel: String, day: Int, isFavourite: Bool) {
self.flagCountry = flagCountry
self.nameCountry = nameCountry
self.typeOfTravel = typeOfTravel
self.day = day
self.isFavourite = isFavourite
}
init(config: NewTravelConfig) {
flagCountry = ""
nameCountry = ""
typeOfTravel = ""
day = 0
isFavourite = false
}
}
class TravelLibrary: ObservableObject {
#Published var testTravels = [
Travel(flagCountry: "USAflag", nameCountry: "États-Unis", typeOfTravel: "Tourisme", day: 0, isFavourite: false),
Travel(flagCountry: "Japanflag", nameCountry: "Japon", typeOfTravel: "Tourisme", day: 0, isFavourite: true),
Travel(flagCountry: "Germanflag", nameCountry: "Allemagne", typeOfTravel: "Tourisme", day: 0, isFavourite: false)
]
}
class NewTravelConfig: ObservableObject {
#Published var nameCountry = ["États-Unis", "Japon", "Allemagne"]
#Published var typeOfTravel = ["Tourisme", "Business"]
#Published var day = 0
#Published var index1 = 0
#Published var index2 = 0
}
Thanks!
Your issue is inside the Travel struct. You have two init() methods, and the issue is in the one where you pass your config.
You are passing the config to it, but not using any of these values from the config.
Here is the correct one:
init(config: NewTravelConfig) {
flagCountry = ""
nameCountry = config.nameCountry[config.index1]
typeOfTravel = config.typeOfTravel[config.index2]
day = config.day
isFavourite = false
}

SwiftUI: How to select multi items(image) with ForEach?

I'm working on my project with the feature of select multiple blocks of thumbnails. Only selected thumbnail(s)/image will be highlighted.
For the ChildView, The binding activeBlock should be turned true/false if a use taps on the image.
However, when I select a thumbnail, all thumbnails will be highlighted.I have come up with some ideas like
#State var selectedBlocks:[Bool]
// which should contain wether or not a certain block is selected.
But I don't know how to implement it.
Here are my codes:
ChildView
#Binding var activeBlock:Bool
var thumbnail: String
var body: some View {
VStack {
ZStack {
Image(thumbnail)
.resizable()
.frame(width: 80, height: 80)
.background(Color.black)
.cornerRadius(10)
if activeBlock {
RoundedRectangle(cornerRadius: 10)
.stroke(style: StrokeStyle(lineWidth: 2))
.frame(width: 80, height: 80)
.foregroundColor(Color("orange"))
}
}
}
BlockBView
struct VideoData: Identifiable{
var id = UUID()
var thumbnails: String
}
struct BlockView: View {
var videos:[VideoData] = [
VideoData(thumbnails: "test"), VideoData(thumbnails: "test2"), VideoData(thumbnails: "test1")
]
#State var activeBlock = false
var body: some View {
ScrollView(.horizontal){
HStack {
ForEach(0..<videos.count) { _ in
Button(action: {
self.activeBlock.toggle()
}, label: {
ChildView(activeBlock: $activeBlock, thumbnail: "test")
})
}
}
}
}
Thank you for your help!
Here is a demo of possible approach - we initialize array of Bool by videos count and pass activated flag by index into child view.
Tested with Xcode 12.1 / iOS 14.1 (with some replicated code)
struct BlockView: View {
var videos:[VideoData] = [
VideoData(thumbnails: "flag-1"), VideoData(thumbnails: "flag-2"), VideoData(thumbnails: "flag-3")
]
#State private var activeBlocks: [Bool] // << declare
init() {
// initialize state with needed count of bools
self._activeBlocks = State(initialValue: Array(repeating: false, count: videos.count))
}
var body: some View {
ScrollView(.horizontal){
HStack {
ForEach(videos.indices, id: \.self) { i in
Button(action: {
self.activeBlocks[i].toggle() // << here !!
}, label: {
ChildView(activeBlock: activeBlocks[i], // << here !!
thumbnail: videos[i].thumbnails)
})
}
}
}
}
}
struct ChildView: View {
var activeBlock:Bool // << value, no binding needed
var thumbnail: String
var body: some View {
VStack {
ZStack {
Image(thumbnail)
.resizable()
.frame(width: 80, height: 80)
.background(Color.black)
.cornerRadius(10)
if activeBlock {
RoundedRectangle(cornerRadius: 10)
.stroke(style: StrokeStyle(lineWidth: 2))
.frame(width: 80, height: 80)
.foregroundColor(Color.orange)
}
}
}
}
}
Final result
Build your element and it's model first. I'm using MVVM,
class RowModel : ObservableObject, Identifiable {
#Published var isSelected = false
#Published var thumnailIcon: String
#Published var name: String
var id : String
var cancellables = Set<AnyCancellable>()
init(id: String, name: String, icon: String) {
self.id = id
self.name = name
self.thumnailIcon = icon
}
}
//Equivalent to your BlockView
struct Row : View {
#ObservedObject var model: RowModel
var body: some View {
GroupBox(label:
Label(model.name, systemImage: model.thumnailIcon)
.foregroundColor(model.isSelected ? Color.orange : .gray)
) {
HStack {
Capsule()
.fill(model.isSelected ? Color.orange : .gray)
.onTapGesture {
model.isSelected = !model.isSelected
}
//Two way binding
Toggle("", isOn: $model.isSelected)
}
}.animation(.spring())
}
}
Prepare data and handle action in your parent view
struct ContentView: View {
private let layout = [GridItem(.flexible()),GridItem(.flexible())]
#ObservedObject var model = ContentModel()
var body: some View {
VStack {
ScrollView {
LazyVGrid(columns: layout) {
ForEach(model.rowModels) { model in
Row(model: model)
}
}
}
if model.selected.count > 0 {
HStack {
Text(model.selected.joined(separator: ", "))
Spacer()
Button(action: {
model.clearSelection()
}, label: {
Text("Clear")
})
}
}
}
.padding()
.onAppear(perform: prepare)
}
func prepare() {
model.prepare()
}
}
class ContentModel: ObservableObject {
#Published var rowModels = [RowModel]()
//I'm handling by ID for futher use
//But you can convert to your Array of Boolean
#Published var selected = Set<String>()
func prepare() {
for i in 0..<20 {
let row = RowModel(id: "\(i)", name: "Block \(i)", icon: "heart.fill")
row.$isSelected
.removeDuplicates()
.receive(on: RunLoop.main)
.sink(receiveValue: { [weak self] selected in
guard let `self` = self else { return }
print(selected)
if selected {
self.selected.insert(row.name)
}else{
self.selected.remove(row.name)
}
}).store(in: &row.cancellables)
rowModels.append(row)
}
}
func clearSelection() {
for r in rowModels {
r.isSelected = false
}
}
}
Don't forget to import Combine framework.

Swift UI | Textfield not reading entered value

I have a textfield which is supposed to log the units of a food product someone has eaten, which is then used to calculate the total number of calories, protein, etc. that the user consumed. But when the value is entered on the textfield, the units variable isn't updated. How can I fix this?
This is my code:
#State var selectFood = 0
#State var units = 0
#State var quantity = 1.0
#State var caloriesInput = 0.0
#State var proteinInput = 0.0
#State var carbsInput = 0.0
#State var fatsInput = 0.0
var body: some View {
VStack {
Group {
Picker(selection: $selectFood, label: Text("What did you eat?")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.white))
{
ForEach(database.productList.indices, id: \.self) { i in
Text(database.productList[i].name)
}
}
.pickerStyle(MenuPickerStyle())
Spacer(minLength: 25)
Text("How much did you have?")
.font(.headline)
.fontWeight(.bold)
.foregroundColor(.white)
.frame(alignment: .leading)
//Textfield not working.
TextField("Units", value: $units, formatter: NumberFormatter())
.padding(10)
.background(Color("Settings"))
.cornerRadius(10)
.foregroundColor(Color("Background"))
.keyboardType(.numberPad)
Button (action: {
self.quantity = ((database.productList[selectFood].weight) * Double(self.units)) / 100
caloriesInput = database.productList[selectFood].calories * quantity
proteinInput = database.productList[selectFood].protein * quantity
carbsInput = database.productList[selectFood].carbs * quantity
fatsInput = database.productList[selectFood].fats * quantity
UIApplication.shared.hideKeyboard()
}) {
ZStack {
Rectangle()
.frame(width: 90, height: 40, alignment: .center)
.background(Color(.black))
.opacity(0.20)
.cornerRadius(15)
;
Text("Enter")
.foregroundColor(.white)
.fontWeight(.bold)
}
}
}
}
}
This is an issue with NumberFormatter that has been going on for a while. If you remove the formatter it updates correctly.
This is a workaround. Sadly it requires 2 variables.
import SwiftUI
struct TFConnection: View {
#State var unitsD: Double = 0
#State var unitsS = ""
var body: some View {
VStack{
//value does not get extracted properly
TextField("units", text: Binding<String>(
get: { unitsS },
set: {
if let value = NumberFormatter().number(from: $0) {
print("valid value")
self.unitsD = value.doubleValue
}else{
unitsS = $0
//Remove the invalid character it is not flawless the user can move to in-between the string
unitsS.removeLast()
print(unitsS)
}
}))
Button("enter"){
print("enter action")
print(unitsD.description)
}
}
}
}
struct TFConnection_Previews: PreviewProvider {
static var previews: some View {
TFConnection()
}
}

Updating Member of an Array

Newbie here.
My problem simplified:
I have a Person struct consisting of 2 strings - first and last name.
An initial array with a few persons (ex. "Bob" "Smith", "Joe" "Johnson", etc.)
A list view showing each member.
Clicking on a row in the list shows a detail view - call it "person card" view - which shows the first name and last name.
I then have a modal view to edit these variables.
Currently the Save button on the modal only closes the modal. However, because I am using bindings on the modal view to the values on the "person card" view, the "person card" view is updated with the changed data when the modal closes.
The list view though still shows the original value(s) and not the updated data (as I expect). I know that I have to add as method to the save function but I'm not sure what. I know how to insert and append to an array but I can't find an update array method.
FYI - The data model I am using is a "store" instance of a class that is an ObservableObject. I have that variable declared as an EnvironmentObject on each view.
Here is the code as requested:
struct PatientData: Identifiable
{
let id = UUID()
var patientName: String
var age: String
}
let patientDataArray: [PatientData] =
[
PatientData(patientName: "Charles Brown", age: "68"),
PatientData(patientName: "Jim Morrison", age: "36"),
]
final class PatientDataController: ObservableObject
{
#Published var patients = patientDataArray
{
struct PatientList: View
{
#EnvironmentObject var patientDataController: PatientDataController
#State private var showModalSheet = false
var body: some View
{
NavigationView
{
List
{
ForEach(patientDataController.patients)
{ patientData in NavigationLink(destination: PatientInfoCard(patientData: patientData))
{ PatientListCell(patientData: patientData) }
}
.onMove(perform: move)
.onDelete(perform: delete)
.navigationBarTitle(Text("Patient List"))
}
struct PatientInfoCard: View
{
#EnvironmentObject var patientDataController: PatientDataController
#State var patientData: PatientData
#State private var showModalSheet = false
var body: some View
{
VStack(alignment: .leading, spacing: 8)
{ // Change to patientDataArray???
Text(patientData.patientName)
.font(.largeTitle)
BasicInfo(patientData: patientData)
Spacer()
.frame(minWidth: 0, maxWidth: .infinity)
}
.padding()
// Can't push Edit button more than once
.navigationBarItems(trailing: Button(action:
{self.showModalSheet = true})
{Text("Edit")})
.sheet(isPresented: $showModalSheet)
{
EditPatientModal(patientData: self.$patientData, showModalSheet: self.$showModalSheet)
.environmentObject(self.patientDataController)
}
}
}
struct EditPatientModal: View
{
#Environment(\.presentationMode) var presentationMode
#EnvironmentObject var patientDataController: PatientDataController
#Binding var patientData: PatientData
#Binding var showModalSheet: Bool
var body: some View
{
NavigationView
{
VStack(alignment: .leading)
{
Text("Name")
.font(.headline)
TextField("enter name", text: $patientData.patientName)
Text("Age")
.font(.headline)
TextField("enter age", text: $patientData.age)
}
.navigationBarTitle(Text("Edit Patient"), displayMode: .inline)
.navigationBarItems(
leading: Button("Cancel")
{ self.cancel() },
trailing: Button("Save")
{ self.save() } )
}
}
private func save()
{
self.presentationMode.wrappedValue.dismiss()
}
Here is my updated code:
class PatientData: ObservableObject, Identifiable
{
let id = UUID()
#Published var patientName = ""
#Published var age = ""
init(patientName: String, age: String)
{
self.patientName = patientName
self.age = age
}
}
let patientDataArray: [PatientData] =
[
PatientData(patientName: "Charles Brown", age: "68"),
PatientData(patientName: "Jim Morrison", age: "36")
]
final class PatientDataController: ObservableObject
{
#Published var patients = patientDataArray
}
struct PatientList: View
{
#EnvironmentObject var patientDataController: PatientDataController
#EnvironmentObject var patientData: PatientData
#State private var showModalSheet = false
var body: some View
{
NavigationView
{
List
{
ForEach(self.patientDataController.patients.indices)
{ idx in
NavigationLink(destination: PatientInfoCard(patientData: self.$patientDataController.patients[idx]))
/*Cannot convert value of type 'Binding<PatientData>' to expected argument type 'PatientData'*/ <-- My one error message; in NavigationLink
{ PatientListCell(patientData: self.$patientDataController.patients[idx]) }
}
.onMove(perform: move)
.onDelete(perform: delete)
.navigationBarTitle(Text("Patient List"))
}
.navigationBarItems(leading: EditButton())
struct PatientInfoCard: View
{
#EnvironmentObject var patientDataController: PatientDataController
#Binding var patientData: PatientData
#State private var showModalSheet = false
var body: some View
{
VStack(alignment: .leading, spacing: 8)
{
Text(patientData.patientName)
.font(.largeTitle)
BasicInfo(patientData: patientData)
Spacer()
.frame(minWidth: 0, maxWidth: .infinity)
}
.padding()
.navigationBarItems(trailing: Button(action:
{self.showModalSheet = true})
{Text("Edit")})
.sheet(isPresented: $showModalSheet)
{
EditPatientModal(patientData: self.$patientData, showModalSheet: self.$showModalSheet)
.environmentObject(self.patientDataController)
}
}
}
struct BasicInfo: View
{
#EnvironmentObject var patientDataController: PatientDataController
#State var patientData: PatientData
var patientDataIndex: Int
{
patientDataController.patients.firstIndex(where: { $0.id == patientData.id })!
}
var body: some View
{
VStack(alignment: .leading, spacing: 8)
{
Text("Age:")
.font(.headline)
Text(patientData.age)
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
struct EditPatientModal: View
{
#Environment(\.presentationMode) var presentationMode
#EnvironmentObject var patientDataController: PatientDataController
#Binding var patientData: PatientData
#Binding var showModalSheet: Bool
var body: some View
{
NavigationView
{
VStack(alignment: .leading)
{
Text("Name")
.font(.headline)
TextField("enter name", text: $patientData.patientName)
Text("Age")
.font(.headline)
TextField("enter age", text: $patientData.age)
}
.navigationBarTitle(Text("Edit Patient"), displayMode: .inline)
.navigationBarItems(
leading: Button("Cancel")
{ self.cancel() },
trailing: Button("Save")
{ self.save() } )
}
}
private func save()
{
self.presentationMode.wrappedValue.dismiss()
}
You are missing 2 things in your code.
Your struct needs to be ObservableObject otherwise any changes happen to it will not get effected and in order for it to be ObservableObject it has to be a class so first change:
class PatientData: ObservableObject, Identifiable
{
let id = UUID()
#Published var patientName: String
#Published var age: String
init(patientName: String, age: String) {
self.patientName = patientName
self.age = age
}
}
I understand you have an environmentObject which is publishing, but it's only publishing changes to the array, meaning adding or removing items but not to individual patientData objects.
2nd thing to change is in your forEach loop you need pass Patient as a Bind and in order to do that you have to loop through indices and then access the data through Bind
NavigationView
{
if(self.patientDataController.patients.count > 0) {
List {
ForEach(self.patientDataController.patients.enumerated().map({$0}), id:\.element.id) { idx, patient in
NavigationLink(destination: PatientInfoCard(patientData: self.$patientDataController.patients[idx])) {
Text(patient.patientName)
}
}
}
.navigationBarItems(leading: EditButton())
} else {
Text("List is empty")
}
}
Let us know if this doesn't work

Resources