简体   繁体   English

在 SwiftUI 中的视图之间传递数据?

[英]Passing data between views in SwiftUI?

I'm trying to pass a few simple data (strings and URL and images) from one view to another view in my swiftui app.我试图在我的 swiftui 应用程序中将一些简单的数据(字符串和 URL 和图像)从一个视图传递到另一个视图。

This simple task proves to be a headache at the moment.事实证明,这个简单的任务目前令人头疼。

This is what I have done so far:这是我到目前为止所做的:

in my firstView I have this:在我的firstView中,我有这个:

struct Book {
    var title: String
    var author: String
}

struct firstView: View {

    @State private var showAudioPlayer = false

    @Binding var tabSelection: Int

    init(tabSelection: Binding<Int>) {        
    
        _tabSelection = tabSelection    
    }    

    var body: some View {

       NavigationView {
          ZStack {

             Text("Tap Here")
             .onTapGesture {
                 self.showAudioPlayer.toggle()
             }        
          }        
      }
      .fullScreenCover(isPresented: $showAudioPlayer, content: secondView.init)

    }

}

And this is what I have in my secondView :这就是我在secondView中的内容:

struct secondView: View {

    @Environment(\.presentationMode) var presentationMode

    var book: Book
    
    var body: some View {
                
        NavigationView {
            
                      ZStack{
                        
                          VStack {                                
                        
                          }
                      }
                    
                      .background(Color.clear)
             
                      .navigationBarTitle("", displayMode: .inline)    
                    
                    .navigationBarItems(
                          leading: Button(action: {
                          
                            presentationMode.wrappedValue.dismiss()
                            
                            
                        }) {
                            Image(systemName: "chevron.down.circle")
                                .foregroundColor(Color.black)                                    
                            
                        })
                      .frame(maxWidth: .infinity, maxHeight: .infinity)
                       .edgesIgnoringSafeArea(.all)
                            
                  }

        .edgesIgnoringSafeArea(.all)
    }

}

However, when I try to compile my code, I get this error:但是,当我尝试编译代码时,出现此错误:

在此处输入图像描述

The error itself doesn't make much sense and I'm stuck.错误本身没有多大意义,我被卡住了。

what am I doing wrong?我究竟做错了什么?

Memberwise Initializers for Structure Types 结构类型的成员初始化器

The compiler takes a peak into the struct and sees two non-optional properties and thinks they should be in the constructor.编译器进入结构并看到两个非可选属性并认为它们应该在构造函数中。 It doesn't know the @Environment is going to magically be set by the framework, so it includes that property.它不知道 @Environment 将由框架神奇地设置,因此它包含该属性。 As @Baglan said above, you are on the hook for the Book and need to pass it in.正如@Baglan 上面所说的,你已经被这本书迷住了,需要把它传进去。

The error message is saying "you asked me to instance audioPlayerView and I need a couple of parameters to make that happen", but you really only need one.错误消息是说“你让我实例化 audioPlayerView,我需要几个参数来实现它”,但你真的只需要一个。

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

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