繁体   English   中英

我的录音机 SwiftUI/Storyboard 应用程序在加载时不断崩溃

[英]My Voice Recorder SwiftUI/Storyboard app keeps crashing when it loads

我正在 SwiftUI 中制作一个录音应用程序,我也在将它与 storyboard 应用程序混合使用。 这是我的第一个 SwiftUI 应用程序,到目前为止,我非常喜欢使用 SwiftUI 进行编码。

但是,我遇到了一个小问题。 当我启动 ContentView 时,我的代码不断崩溃。

我正在从这里做教程: https://www.blckbirds.com/post/voice-recorder-app-in-swiftui-1

我已经构建了一个 Swift Storyboard 应用程序,我想将 SwiftUI 录音机与之前构建的应用程序集成。 我按照本教程进行操作: Is there any way to use storyboard and SwiftUI in the same iOS ZA3B5EBD8A1E9EBF74A17DAD03 project? .

这是我的代码:

import UIKit
import SwiftUI

struct ContentView: View {

    @ObservedObject var audioRecorder: AudioRecorder

  var body: some View {
      VStack {
        RecordingsList(audioRecorder: audioRecorder)

        if audioRecorder.recording == false {
            Button(action: {print("Start Recording")}) {
                Image(systemName: "circle.fill")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 100, height: 100)
                .clipped()
                .foregroundColor(.red)
                .padding(.bottom, 40)
            } //button action ends here
        } else {
            Button(action: {print("Stop Recording")}) {
                Image(systemName: "stop.fill")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 100, height: 100)
                .clipped()
                .foregroundColor(.red)
                .padding(.bottom, 40)
            } //button stop ends here
        } //if and else ends here
        navigationBarTitle("Voice Recorder")
      } //VStack Ends here
    } //body view ends here
} //Struct ContentView Ends here

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(audioRecorder: AudioRecorder())
    }
}

class ChildHostingController: UIHostingController<ContentView> {

    required init?(coder: NSCoder) {
        super.init(coder: coder,rootView: ContentView());
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

这行得通,但是当我开始遵循录音机教程并且您必须输入“@ObservedObject var audioRecorder:AudioRecorder”时,“class ViewHostingController:UIHostingController”中会出现错误,特别是在“super.init(coder : coder,rootView: ContentView());”,其中 Xcode 表示“调用中缺少参数‘audioRecorder’的参数”。 当我关注 Xcode 的 AutoFix 时,它会将该行代码更改为“super.init(coder: coder,rootView: ContentView(audioRecoder: AudioRecorder()));”。 不会出现错误,但运行此程序会导致应用程序崩溃并出现以下代码:“线程 1:EXC_BAD_ACCESS (code=2, address=0x7ffeeec48ec8)”。

任何帮助解决这个问题真的很棒:非常感谢! :)

它应该是.navigationBarTitle("Voice Recorder") ,(注意句号),并且应该在VStack关闭之后(低一行)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM