所以由于某种原因,当我尝试在ZStack中实现NavigationView时,我的应用程序崩溃了。
当我按下按钮时,我试图显示一个弹出窗口。不幸的是,它不起作用。
这是我的代码:
// gymappswiftuivone
//
// Created by Yonatan Bensimon on 2019-12-29.
// Copyright © 2019 Yonatan Bensimon. All rights reserved.
//
import SwiftUI
import Combine
struct Log: View {
@Environment(\.managedObjectContext) var managedObjectContext //The Context
@FetchRequest(fetchRequest: ExerciseForWorkout.getAllExerciseForWorkout()) var loggedExercise: FetchedResults < ExerciseForWorkout > //The request in memory
@EnvironmentObject
var userData: UserData
@State var newLoggedExercise = ""
@State private var newReps: String = ""
@State private var newWeight: String = ""
@State private var isAppeared: Bool = false
@State var float: CGFloat = 20.0
var body: some View {
ZStack {
VStack {
NavigationView { //ERROR
List {
Section(header: Text("Add Exercise")) {
HStack {
TextField("Exercise", text: $newLoggedExercise)
.disabled(true)
.onTapGesture {
self.isAppeared = true
}
TextField("Weight", text: $newWeight)
.keyboardType(.numberPad)
.onReceive(Just(newWeight)) {
newValue in
let filtered = newValue.filter {
"0123456789".contains($0)
}
if filtered != newValue {
self.newWeight = filtered
}
}
TextField("Reps", text: $newReps)
.keyboardType(.numberPad)
.onReceive(Just(newReps)) {
newValue in
let filtered2 = newValue.filter {
"0123456789".contains($0)
}
if filtered2 != newValue {
self.newReps = filtered2
}
}
Button(action: {
let loggedExerciseNew = ExerciseForWorkout(context: self.managedObjectContext)
loggedExerciseNew.name = self.newLoggedExercise
loggedExerciseNew.reps = self.newReps
loggedExerciseNew.dateCreated = Date()
loggedExerciseNew.weight = self.newWeight
do {
try self.managedObjectContext.save()
} catch {
print("Error while saving !!!")
print(error)
}
self.newLoggedExercise = ""
}) {
Image(systemName: "plus.circle.fill")
.foregroundColor(Color.green)
.imageScale(.large)
}
}
} //Sectio
Section(header: sectionHeader()) {
ForEach(self.loggedExercise, id: \.self) {
log in
loggedExerciseTab(name: log.name, reps: log.reps, weight: log.weight)
}.onDelete {
indexSet in
let deleteEx = self.loggedExercise[indexSet.first!]
self.managedObjectContext.delete(deleteEx)
do {
try self.managedObjectContext.save()
} catch {
print("Error while saving !!!")
print(error)
}
}
}
} //List
.navigationBarTitle(Text("Log"))
.navigationBarItems(trailing: EditButton())
} //Navigation View
Button(action: {}) {
completeWorkout()
.padding()
}
}
//ZSTACK
if $isAppeared.wrappedValue {
ZStack {
Color.black.opacity(0.4)
.edgesIgnoringSafeArea(.vertical)
VStack(spacing: 20) {
Text("Choose Exercise")
.bold().padding()
.frame(maxWidth: .infinity)
.background(Color.blue)
.foregroundColor(Color.white)
Spacer()
Button(action: {
self.isAppeared = false
}) {
Picker(selection: $newLoggedExercise) {
ForEach(0... < exerciseData.count) {
Text(exerciseData[$0].name).tag(exerciseData)
}
}
}.padding()
}
.frame(width: CGFloat(300), height: CGFloat(200))
.background(Color.white)
.cornerRadius(float).shadow(radius: float)
}
} //IF
} //ZStack Doe
} //BODY
} //VIEW
struct Log_Previews: PreviewProvider {
static
var previews: some View {
Log()
}
}
当我声明NavigationView时,Xcode给我这个错误:
Generic parameter 'Content' could not be inferred
Explicitly specify the generic arguments to fix this issue
我已经尝试了一切,但仍然想不通! 谁能找到我的错误/错误,如果找不到,您对我应该如何实现弹出窗口有更好的想法?