ios 如何在swift中在类和视图之间切换时保持对象的活动状态?

jaxagkaj  于 2022-12-05  发布在  iOS
关注(0)|答案(2)|浏览(124)

我有一个名为AudioRecorder的类和一个名为RecorderView的视图。

  • startRecording()--〉开始录制并获取录制开始时间。
  • stopRecording()--〉停止记录并获取记录停止时间。
  • saveToCoreData()--〉将开始时间、停止时间和额定值保存到核心数据

它的工作原理是,RecorderView允许用户通过调用AudioRecorder中的函数来开始和停止记录。一旦用户停止记录,就会显示一个名为RatingView的新视图。用户在RatingView中提供一个评级,然后点击提交。通过选择提交,将调用RecorderView中的saveToCoreData,其中包含用户提供的评级。这里的问题是,当视图调用saveToCoreData时,startTimestopTime丢失了。这就是为什么它们是“nil”,只有rating有正确的值。我如何保持startTimestopTime的活动状态以备下次使用?有没有办法解决这个问题?

import SwiftUI

struct RecorderView: View {
    @ObservedObject var audioRecorder: AudioRecorder
    @State private var showRatingView = false
    
    var body: some View {
        VStack {
            if audioRecorder.recording == false {
                Button(action: {self.audioRecorder.startRecording()}) {
                    Image(systemName: "circle.fill")
                }
            } else {
                Button(action: {self.audioRecorder.stopRecording(); showRatingView = true}) {
                    Image(systemName: "stop.fill")
                }
            }
            VStack {
                if showRatingView == true {
                    NavigationLink(destination: SurveyRatingView(rating: 1, audioRecorder: AudioRecorder()), isActive: self.$showRatingView) { EmptyView()}
                }
            }
        }
    }
}


struct RatingView: View {
    
    @Environment(\.managedObjectContext) var moc
    @State var rating: Int16
    @ObservedObject var audioRecorder: AudioRecorder
    
    @State private var displayNewView: Bool = false
    
    var body: some View {
        VStack {
            
            Text("How was the recording experience?")
//          RatingView(rating: $survey)
            Button(action: {
                self.audioRecorder.saveToCoreData(rating: rating)
            }) {
                Text("Submit Response")
            }
        }
        .navigationBarBackButtonHidden(true)
    }
}

AudioRecorder类如下所示

class AudioRecorder:  NSObject, ObservableObject {
    @Published var startTime: Date
    @Published var stopTime: Date
    
    func startRecording() -> Date {
        self.startTime = Date()
        //Start recording related code here
    }
    
    func stopRecording() -> Date{
        // Stop recording related code here
        self.stopTime = Date()
    }
    
    
    func saveToCoreData(rating: Int16){
        let aRec = AudioRec(context: moc)
        aRec.uuid = UUID()
        aRec.startTime = self.startTime //This is nil
        aRec.stopTime = self.stopTime  //This is nil
        aRec.rating = rating  //Only this has a proper value
        
        try moc.save()
    }
}
mum43rcc

mum43rcc1#

在“showRatingView”VStack中,您正在传递AudioRecorder的新示例。您应该传递已有的示例:

VStack {
    if showRatingView == true {
        NavigationLink(destination: SurveyRatingView(rating: 1, audioRecorder: self.audioRecorder), isActive: self.$showRatingView) { EmptyView()}
    }
}
mwkjh3gx

mwkjh3gx2#

没有代码我只是猜测你的设置是什么。
解决此问题的一种方法是将startTimestopTime值存储在AudioRecorder类本身中,而不是仅在startRecording()stopRecording()函数中本地使用它们。这样,当调用saveToCoreData()函数时,这些值仍然可用。
例如,您可以将两个属性加入至AudioRecorder类别,以储存startTime和stopTime值:

class AudioRecorder {
    var startTime: Date?
    var stopTime: Date?

    func startRecording() {
        self.startTime = Date()
        // other code here...
    }

    func stopRecording() {
        self.stopTime = Date()
        // other code here...
    }

    func saveToCoreData(rating: Int) {
        // use self.startTime and self.stopTime here...
        // other code here...
    }
}

或者,您可以将startTime和stopTime值作为参数传递给saveToCoreData()函数,以便在调用该函数时视图可以提供这些值。

class AudioRecorder {
     func startRecording() -> Date {
        return Date()
        // other code here...
    }

    func stopRecording() -> Date {
        return Date()
        // other code here...
    }

    func saveToCoreData(startTime: Date, stopTime: Date, rating: Int) {
        // use startTime and stopTime here...
        // other code here...
    }
}

在这种情况下,视图需要存储startRecording()和stopRecording()函数返回的startTime和stopTime值,然后在调用saveToCoreData()函数时将这些值传递给该函数。
我希望这对你有帮助!

相关问题