Saturday, November 21, 2026
HomeiOS Developmentios - Type reverts and reloads knowledge on unwind

ios – Type reverts and reloads knowledge on unwind

[ad_1]

So I’ve a tricky SwiftUI type scenario which I am attempting to resolve.

I am attempting to reuse a Type in each the creation and the enhancing of information relying on whether or not I go a Mannequin into it or not.

What I’ve for the time being is a working model, but when the person modifications a few of the TextFields earlier than choosing some gadgets within the subsequent Navigation screens, then on the unwind it undoes all of the customers edits.

Nonetheless, if I take away the .onAppear then the info does not load into the edit mode.

If I do not add within the if !UserDefaults.customary.bool(forKey: "formItems") {} block, then every time the info is segued to the checklist, and again it resets to the unique mannequin.

Is there a manner that I can have each the info load if there’s a mannequin handed in, and up to date if edited on the unwind – however not reset it.

The one manner I can suppose – however I do not suppose it’s a good or finest manner – is to retailer the formItems array right into a UserDefault, then delete it on full pop to root.

I’ve additionally tried utilizing an init() { vm.loadData(mannequin: mannequin) } on the ContentView however I get the purple error:

Accessing StateObject’s object with out being put in on a View. This can create a brand new occasion every time.

I’m on iOS 14+ so I’m not utilizing any of the brand new APIs.

With the UserDefault block
With the UserDefault block

Without the UserDefault block
With out the UserDefault block


Check code

Entrance

@fundamental
struct testApp: App {
  var physique: some Scene {
    WindowGroup {
      NavigationView { // <-- demo use solely
        NavigationLink("Go to type") {
          ContentView()
        }
        .navigationTitle("Principal display")
      }
    }
  }
}

Mannequin

struct Mannequin {
  let id = UUID().uuidString
  let identify: String
  let numbers: [Int]
}

Type view

struct ContentView: View {
  @StateObject personal var vm = ViewModel()
  var mannequin: Mannequin? = Mannequin(identify: "Michael", numbers: [1, 5, 7])
  var physique: some View {
  Type {
    TextField("Identify", textual content: $vm.formName)
      Part {
        NavigationLink(vm.formItems.isEmpty ? "Select gadgets" : vm.formItemsName) {
          ListItemsView()
            .environmentObject(vm)
        }
      } footer: { Textual content("(vm.formItems.description)") }
    }
    .navigationTitle("Add new")
    .onAppear { vm.loadData(mannequin: mannequin) }
  }
}

Type sub-view

struct ListItemsView: View {
  @EnvironmentObject personal var vm: ViewModel
  var physique: some View {
  Type {
    Checklist(0..<10) { index in
      Textual content("(index)")
        .onTapGesture {
          vm.formItems.append(index)
        }
    }
  }
  }
}

View Mannequin

class ViewModel: ObservableObject {
  @Revealed var formName: String = ""
  @Revealed var formItems: [Int] = []
  @Revealed var formItemsName: String = ""
  func loadData(mannequin: Mannequin?) {
    if let mannequin = mannequin {
      formName = mannequin.identify
      if !UserDefaults.customary.bool(forKey: "formItems") {
        formItems = mannequin.numbers
        UserDefaults.customary.set(true, forKey: "formItems")
      }
    }
    formItemsName = getNames(from: formItems)
  }
  func getNames(from gadgets: [Int]) -> String {
    var output: [String] = []
    for merchandise in gadgets {
      output.append("(merchandise)")
    }
    return output.joined(separator: ", ")
  }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments