Ok, here's the code. Part of the first view. You can see that I pass the ship name to a view in .sheet
struct ShipEditorView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: Ship.getAllShips()) var ships:FetchedResults<Ship>
@State private var newShip = ""
@State var shipSelected = ""
@State var showingAddShipAlert = false
@State var showingShipEditor = false
@State var edit = false
var body: some View {
ZStack(alignment: .bottomTrailing){
VStack{
List{
ForEach(self.ships) { ship in
if self.edit == true {
Button(action: {
print("Opens Ship Editor")
self.shipSelected = ship.shipName!
self.showingShipEditor = true
}) {
Text(ship.shipName!)
}
.sheet(isPresented: self.$showingShipEditor) { ShipEditingView(shipName: self.shipSelected) }
} else {
NavigationLink(destination: ProductEditorView(shipName: ship.shipName!)) {
Text(ship.shipName!)
}
}
} .onDelete {indexSet in ...
Then the second view. When this view shows up, the ship textfield is blank. I even tried printing the shipName and shipText and nothing shows up.
struct ShipEditingView: View {
let shipName: String
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
@State var context: NSManagedObjectContext!
@State var shipText = ""
var body: some View {
NavigationView{
VStack{
Form{
Section(header: Text("Rename Ship")) {
TextField("Titanic", text: $shipText)
.font(.title)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
Button(action: {
self.saveEdits()
self.mode.wrappedValue.dismiss()
}) {
Text("Done")
.font(.title)
}
Spacer()
}
.navigationBarTitle(Text("Rename Ship"))
.onAppear() {
self.shipText = self.shipName
}
}.navigationViewStyle(StackNavigationViewStyle())
}
func saveEdits() { ...
I'm wondering if it's a bug with SwiftUI. Running Beta 8 and I've noticed that if I tap on the ship I want to edit and dismiss the sheet a few times, then the ship name will show up in the textfield and never fails with different ships after that.