English 中文(简体)
从儿童角度返回后重新选取
原标题:Refreshing picker after returning from child view

我想添加一个新的清单标。 为此,要求你从一艘船主中挑选一艘船舶物体。 如果尚未设立船舶,则促使你先填上一张船号,然后在船只上加后撤销该船。

如果我开车,船只已经存在,一切照旧。 然而,如果还没有船只从船上挑选,则在船上打上新号(NewView)号船,并正确地向船级添加新物体。 当它返回新通风港时,它现在向该船检查后发现该船。 页: 1 如果I ("vessel= (vessel)”>,此时,I 收到vessel = <Vessel: 0x6000017d07c0> (entity: <null> id: (null); data: <fault>),并试图挽救 app。

However if I click on the picker and re-select the item that looks like its already selected, then it works fine. How can I force the NewInventoryView to refresh the picker choices for vessel once it returns from adding one on NewVesselView?

我的守则如下:

import SwiftUI

struct InventoryNewView: View {
    @Environment(.managedObjectContext) private var viewContext
    
    @State private var showSheetVessel = false
    
    @FetchRequest(
        sortDescriptors: [SortDescriptor(.name)])
    var vessels:FetchedResults<Vessel>
    
    @Environment(.dismiss) var dismiss
    @State var name = ""
    @State var vessel = Vessel()
    
    var body: some View {
        NavigationView {
            ScrollView {
                VStack(alignment: .leading){
                    Grid(alignment: .leading) {
                        GridRow {
                            Text("Name:")
                            TextField("Inventory item", text: $name)
                        }
                        .padding(.bottom, 5)
                        GridRow {
                            Text("Assigned to:")
                            if vessels.isEmpty == false {
                                Picker("", selection: $vessel) {
                                    ForEach(vessels) { (vessel: Vessel) in
                                        Text(vessel.name)
                                            .tag(vessel)
                                    }
                                }
                            }
                            else {
                                Text("Add Vessel")
                                    .fontWeight(.semibold)
                                    .foregroundColor(Color.red)
                            }
                            Button(action: {
                                self.showSheetVessel.toggle()
                            }) {
                                Image(systemName: "plus.circle")
                                    .font(.footnote)
                            }.sheet(isPresented: $showSheetVessel) {
                                VesselNewView()
                            }
                        }
                        .padding(.bottom, 5)
                    }
                }
                .navigationTitle("Add New Inventory")
                .navigationBarItems(
                    trailing:
                        Button("Add") {
                            addInventory()
                            dismiss()
                        }
                        .disabled(name.isEmpty)
                )
            }
        }
        
        func addInventory() {
            // Add the item into Core Data
            let inventory = Inventory(context: viewContext)
            inventory.id = UUID()
            inventory.name = name
            inventory.vessel = vessel
            vessel.addToInventory(inventory)
            
            
            do {
                try viewContext.save()
            }
            catch {
            }
            
        }
    }
}
问题回答

更新所选的“条形表”

 .onChange(of: vessels.last) { 
    if let vesl = vessels.last { 
       vessel = vesl 
    } 
  } 
 

缩略语 观点......}。

注:Navigation 意见译本,请使用<代码>NavigationStack





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签