我想在从NavigationLink取消子视图之后,从一个具有成功的子视图,再从另一个具有XCode中的“ EXC_BAD_ACCESS(code = 2 ...”)的子视图,来更新主NavigationView。
我通过@Binding var来做到这一点,我从子视图更改了它的值。我认为问题与CoreData有关,但我不知道在哪里以及为什么。
有什么主意吗?
MainView:
struct ProgramView: View {
@State var preliminaryViewed: Bool = Program2s.shared.program2s.preliminaryViewed
@State var programs = ProgramStatess.shared.programStats
@State var states: [Int] = [0, 0, 0, 0]
func programState(number: Int){
if programs[number].startDate != nil {
if programs[number].completed {
states[number] = 2
}
else{
states[number] = 1
}
}
else{
states[number] = 0
}
}
var body: some View {
NavigationView {
VStack(alignment: .leading){
ForEach(programs, id: \.self){ program in
Group {
NavigationLink(destination:
{
VStack{
if program.order == 0 {
if self.states[Int(program.order)] == 1 {
Program1View(state: self.$states[Int(program.order)])
.navigationBarTitle(Text("\(program.name!)"), displayMode: .inline)
}
else {
Program1ResultView()
.navigationBarTitle(Text("\(program.name!)(Splněno) - shrnutí"), displayMode: .inline)
}
}
else if program.order == 1 {
if self.states[Int(program.order)] == 1 {
if !self.preliminaryViewed {
AnyView(Program2PreliminaryView(preliminaryViewed: self.$preliminaryViewed))
.navigationBarTitle(Text("\(program.name!)"), displayMode: .inline)
}
else {
AnyView(Program2View(state: self.$states[Int(program.order)]))
.navigationBarTitle(Text("\(program.name!)"), displayMode: .inline)
}
}
else{
AnyView(Program2View(state: self.$states[Int(program.order)]))
.navigationBarTitle(Text("\(program.name!)"), displayMode: .inline)
}
}
else {
if self.states[Int(program.order)] == 1 {
AnyView(Program1View(state: self.$states[Int(program.order)]))
.navigationBarTitle(Text("\(program.name!)"), displayMode: .inline)
}
else{
AnyView(Program1View(state: self.$states[Int(program.order)]))
.navigationBarTitle(Text("\(program.name!)"), displayMode: .inline)
}
}
} // end of VStack
}()
) {
VStack(alignment: .leading){
if self.states[Int(program.order)] == 0 {
Text("\(program.name!)")
.fontWeight(.bold).font(.headline)
Divider()
Text("Closed")
}
else if self.states[Int(program.order)] == 1 {
Text("\(program.name!)")
.fontWeight(.bold).font(.headline)
.foregroundColor(Color(.systemYellow))
Divider()
Text("Opened")
}
else {
Text("\(program.name!)")
.fontWeight(.bold).font(.headline)
.foregroundColor(Color(UIColor.systemGray))
Divider()
Text("Finished")
}
}
.padding()
} // end of navlink
.buttonStyle(PlainButtonStyle())
.disabled(self.states[Int(program.order)] == 0)
Spacer()
} // end of group
} // end of foreach
} // end of VStack
.padding()
.navigationBarTitle("Program")
.onAppear(){
for value in 0...3 {
print("self.states[\(value)]=\(self.states[value])")
}
}
} // end of Navigationview
.onAppear(perform: {
self.changeColor()
for value in 0...3 {
self.programState(number: value)
}
})
}
更改“ self.state = 2”后具有成功的子视图
struct Program2View: View {
@Binding var state: Int
@State private var page: Int = 0
@State private var disableSwipe: Bool = false
@State private var pages: Int = 7
@State var progressValue: Float = 1.0/7.0
@State var progressChange: Float = 1.0/7.0
@State var showingAlert: Bool = false
@Environment(\.presentationMode) var presentationMode
let program2 = Program2s.shared
let programStates = ProgramStatess.shared
@ObservedObject var stopWatch = StopWatch()
var body: some View {
ZStack{
VStack{
ProgressBar(value: $progressValue).frame(height: 20)
.padding()
PageView([
AnyView(
VStack{
Text("Bla bla bla.")
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{
Text("Bla bla bla.")
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{
Text("Bla bla bla.")
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{
Text("Bla bla bla.")
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{
Text("Bla bla bla.")
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{
Text("Bla bla bla.")
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{
Spacer()
Text("Bla bla bla.")
.fixedSize(horizontal: false, vertical: true)
.padding(.bottom)
Text("Bla bla bla.")
Spacer()
}
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
)
], currentPage: $page, disableSwipe: $disableSwipe)
} // end of VStack
VStack{
Spacer()
HStack{
Spacer()
if (page-1 > -1 ){
Button(action: ({
self.page=self.page-1
})){
Image(systemName: "arrow.left")
.foregroundColor(Color(.systemGray))
.padding()
}
}
if (page+1 < pages ){
Button(action: ({
self.page=self.page+1
})){
Text("Continue")
}
}
if (page == 6 ){
Button(action: ({
if self.program2.program2s.programCompleted == nil {
self.showingAlert = true
self.stopWatch.pause()
}
else {
self.presentationMode.wrappedValue.dismiss()
}
})){
Text("Finish")
}
}
} // end of HStack
} //end of VStack
.padding()
} // end of ZStack
.alert(isPresented: $showingAlert, content:{
Alert(title: Text("Bla bla bla"),
message: Text("Bla bla bla"),
primaryButton: .default(Text("Continue")) {
if self.program2.program2s.programCompleted == nil{
self.program2.setProgram2(relaxDurationInSec: Double(self.stopWatch.stopWatchSeconds))
self.programStates.setProgramStateCompleted(program: 2)
}
self.state = 2 // HERE IT IS
self.presentationMode.wrappedValue.dismiss()
},
secondaryButton: .cancel(Text("Back"))
)
})
.onAppear(){
// START STOPWATCH
if self.program2.program2s.programCompleted == nil {
if self.stopWatch.stopWatchSeconds == 0 {
self.stopWatch.start()
}
}
}
.onDisappear(){
// STOP STOPWATCH
if !self.stopWatch.isPaused() {
self.stopWatch.pause()
self.stopWatch.reset()
}
}
}
更改“ self.state = 2”后出现错误的SubView
struct Program1View: View {
@Binding var state: Int
@EnvironmentObject var sliderData: SliderData
@State private var page: Int = 0
@State private var disableSwipe: Bool = true
@State private var pages: Int = 6
@State var progressValue: Float = 1.0/6.0
@State var progressChange: Float = 1.0/6.0
@State var showingAlert: Bool = false
@State var target: String = ""
@State var target_saveChangesInstantly: Bool = false
@State var completion: String = ""
@State var completion_saveChangesInstantly: Bool = false
@State var satisfiability: Float = 0
@State var reason: String = ""
@State var reason_saveChangesInstantly: Bool = false
@State var deadline: String = ""
@State var deadline_saveChangesInstantly: Bool = false
@State var summarizedTarget: String = ""
@State var summarizedTarget2: String = ""
@Environment(\.presentationMode) var presentationMode
let program1 = Program1s.shared
let programStates = ProgramStatess.shared
var nextIsDisabled: Bool {
switch self.page {
case 0 : do {
return target == "" ? true : false
}
case 1: do {
return completion == "" ? true : false
}
case 2: do {
return satisfiability < 6.0 ? true : false
}
case 3: do {
return reason == "" ? true : false
}
case 4: do {
return deadline == "" ? true : false
}
case 5: do {
return summarizedTarget == "" ? true : false
}
default: do {
return true
}
}
}
var nextButtonColor: Color {
return nextIsDisabled ? Color(.systemGray) : app_green
}
var submitButtonColor: Color {
return summarizedTarget == "" ? Color(.systemGray) : app_green
}
var body: some View {
ZStack {
VStack {
ProgressBar(value: $progressValue).frame(height: 20)
.padding()
PageView([
AnyView(
VStack{ // Slide 1
HStack{
Text("Blablabla")
Spacer()
}
HStack{
MultilineTextView(text: $target, text_saveChangesInstantly: $target_saveChangesInstantly)
}
.keyboardAdaptive(posun: 245)
.padding()
.onAppear(){
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView(
VStack{ // Slide 2
HStack{
Text("Blablabla")
Spacer()
}
HStack{
MultilineTextView(text: $completion, text_saveChangesInstantly: $completion_saveChangesInstantly)
}
}
.keyboardAdaptive(posun: 245)
.padding()
.onAppear(){
if self.target == "" {
self.page = 0
}
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView( // Slide 3
VStack(spacing: 50){
HStack{
Text("Blablabla")
Spacer()
}
VStack{
HStack{
Slider(value: $sliderData.sliderValue, in: 1...10, step: 1, onEditingChanged: { (changed) in
// Editing Changed
self.satisfiability = self.sliderData.sliderValue
})
.accentColor(Color(.systemYellow))
}
HStack(){
ForEach(1 ..< 11) { number in
Text("\(number)")
.frame(maxWidth: .infinity)
.onTapGesture {
self.satisfiability = Float(number)
self.sliderData.sliderValue = Float(number)
}
}
}
}
HStack{
if self.satisfiability > 0.0 && self.satisfiability < 6.0 {
Text("Blablabla")
}
Spacer()
}
Spacer()
} // end of VStack
.padding()
.onAppear(){
if self.completion == "" {
self.page = 1
}
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView( // Slide 4
VStack{
HStack{
Text("Blablabla")
Spacer()
}
HStack{
MultilineTextView(text: $reason, text_saveChangesInstantly: $reason_saveChangesInstantly)
}
}
.keyboardAdaptive(posun: 245)
.padding()
.onAppear(){
if self.satisfiability < 6.0 {
self.page = 2
}
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView( // Slide 5
VStack{
HStack(alignment: .firstTextBaseline){
Text("Blablabla")
Spacer()
}
HStack{
MultilineTextView(text: $deadline, text_saveChangesInstantly: $deadline_saveChangesInstantly)
}
}
.keyboardAdaptive(posun: 245)
.padding()
.onAppear(){
if self.reason == "" {
self.page = 3
}
self.progressValue = Float(self.page+1)*self.progressChange
}
),
AnyView( // Slide 6 - final
ScrollView{
VStack{
HStack{
Text("1.Blablabla")
Spacer()
}
HStack{
Text("\(target)")
Spacer()
}
}.padding(.leading).padding(.bottom)
VStack{
HStack{
Text("2.Blablabla")
Spacer()
}
HStack{
Text("\(completion)")
Spacer()
}
}.padding(.leading).padding(.bottom)
VStack{
HStack{
Text("3. Blablabla")
Spacer()
}
HStack{
Text("\(satisfiability, specifier: "%.0f") / 10")
Spacer()
}
}.padding(.leading).padding(.bottom)
VStack{
HStack{
Text("4. Blablabla")
Spacer()
}
HStack{
Text("\(reason)")
Spacer()
}
}.padding(.leading).padding(.bottom)
VStack{
HStack{
Text("5. Blablabla")
Spacer()
}
HStack{
Text("\(deadline)")
Spacer()
}
}.padding(.leading).padding(.bottom)
VStack{
HStack{
Text("Blablabla")
Spacer()
}
HStack{
TextField("Blablabla" text: $summarizedTarget2, onEditingChanged: { (changed) in
// Editing Changed
if self.summarizedTarget2.count > 69 {
self.summarizedTarget2=String(self.summarizedTarget2.prefix(69))
}
}) {
//Editing Commited
self.summarizedTarget = self.summarizedTarget2
}
.textFieldStyle(RoundedBorderTextFieldStyle())
Spacer()
}
}.padding()
} // end of Scrollview
.keyboardAdaptive(posun: 120)
.onAppear(){
if self.deadline == "" {
self.page = 4
}
self.progressValue = Float(self.page+1)*self.progressChange
}
)
], currentPage: $page, disableSwipe: $disableSwipe)
} // end of VStack
VStack{
Spacer()
HStack{
Spacer()
if (page-1 > -1 ){
Button(action: ({ // Button "Back"
self.page=self.page-1
})){
Image(systemName: "arrow.left")
}
}
if (page+1 < pages ){
Button(action: ({
self.page=self.page+1
})){
Text("Continue")
}.disabled(nextIsDisabled)
}
if (page == 5 ){
Button(action: ({
self.showingAlert = true
})){
Text("Finish")
}.disabled(summarizedTarget == "")
}
} // end of HStack
} //end of VStack
.padding()
} // end of ZStack
.alert(isPresented: $showingAlert, content:{
Alert(title: Text("Blablabla"),
message: Text("Blablabla"),
primaryButton: .default(Text("Continue")) {
self.program1.addProgram1(target: self.target, completion: self.completion, satisfiability: self.satisfiability, reason: self.reason, deadline: self.deadline, sumarizedTarget: self.summarizedTarget)
self.programStates.setProgramStateCompleted(program: 1)
self.state = 2 // HERE IT IS, Starting problem finished with ERROR
self.presentationMode.wrappedValue.dismiss()
},
secondaryButton: .cancel(Text("Edit"))
)
})
} // end of body