ItemDetailView.swift
iOS_App/To-Do-List-Demo/To-Do-List-Demo/ItemDetailView.swift
//
// ItemDetailView.swift
// To-Do-List-Demo
//
// Created by Nelson Lai on 2026/6/25.
//
import SwiftUI
struct ItemDetailView: View {
@Environment(\.dismiss) private var dismiss
@Bindable var item: Item
@State private var subject = ""
@State private var itemDescription = ""
private var trimmedSubject: String {
subject.trimmingCharacters(in: .whitespacesAndNewlines)
}
private var trimmedDescription: String {
itemDescription.trimmingCharacters(in: .whitespacesAndNewlines)
}
private var canSave: Bool {
!trimmedSubject.isEmpty
}
var body: some View {
Form {
Section("Subject") {
TextField("Enter subject", text: $subject)
}
Section("Description") {
TextField("Enter description", text: $itemDescription, axis: .vertical)
.lineLimit(3...6)
}
Section("Created") {
Text(item.timestamp, format: Date.FormatStyle(date: .complete, time: .standard))
}
}
.navigationTitle(trimmedSubject.isEmpty ? item.subject : trimmedSubject)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Save") {
saveChanges()
dismiss()
}
.disabled(!canSave)
}
}
.onAppear {
loadFromItem()
}
}
private func loadFromItem() {
subject = item.subject
itemDescription = item.itemDescription
}
private func saveChanges() {
item.subject = trimmedSubject
item.itemDescription = trimmedDescription
}
}
Related articles
AddItemView.swift
AddItemView.swift — swift source code from the iOS App learning materials (iOS_App/To-Do-List-Demo/To-Do-List-Demo/AddItemView.swift).
Read article →ContentView.swift
ContentView.swift — swift source code from the iOS App learning materials (iOS_App/To-Do-List-Demo/To-Do-List-Demo/ContentView.swift).
Read article →Item.swift
Item.swift — swift source code from the iOS App learning materials (iOS_App/To-Do-List-Demo/To-Do-List-Demo/Item.swift).
Read article →To_Do_List_DemoApp.swift
To_Do_List_DemoApp.swift — swift source code from the iOS App learning materials (iOS_App/To-Do-List-Demo/To-Do-List-Demo/To_Do_List_DemoApp.swift).
Read article →To_Do_List_DemoTests.swift
To_Do_List_DemoTests.swift — swift source code from the iOS App learning materials (iOS_App/To-Do-List-Demo/To-Do-List-DemoTests/To_Do_List_DemoTests.swift).
Read article →To_Do_List_DemoUITests.swift
To_Do_List_DemoUITests.swift — swift source code from the iOS App learning materials (iOS_App/To-Do-List-Demo/To-Do-List-DemoUITests/To_Do_List_DemoUITests.swift).
Read article →