3

This code works very well for me. But I need to add 2 more .sheet in this page. When I try to other solutions list cell doesnt pass to data correctly. How do I improve this code for 3 sheet?

@State var selectedUser: User?

     List...
        UserCell(user: user)
            .onTapGesture {
                 self.selectedUser = user
                        }

.sheet(item: self.$selectedUser) { user in
    DetailView(user: user)
}
Robert
  • 109
  • 7
  • 1
    What do you mean by three sheets? Would you show demo? In general you can use same one sheet, but just pass different items inside, eg. enum with different stored types and show view corresponding to that data. It is really very rare case when in one body might be needed several sheets. – Asperi Sep 26 '20 at 19:33
  • in View, I have a list and data. First: I want to add data to the list but I dont want to use navigationlink. I want to open the sheet with a button in the navigationBarItems.(AdItemView). Second: when I tap to list cell, I want to show DetailView (This is a different view and need to pass cell data). Third: I want to edit cell data. When I tab to context menu, I want to open EditView. Add and edit View is different because I have some reason in my project. – Robert Sep 26 '20 at 19:46

1 Answers1

2

NavigationView has only one sheet per view, so data in sheet instead multiple sheets as in your list; add one sheet to your view change data on tap, like in below code;

 enum SheetType {
    case preview
    case edit
    case yourAnyChoice
 }

struct ContentView:View{
  @State var selectedUser:String = "" 
  @State var showingDetail = false
  @State var sheetType:SheetType = SheetType.preview
  var body: some View {
  List(userList){in user
      HStack{
            Button(action: {
                self.selectedUser = user.name
                self.sheetType = .preview
                self.showingDetail.toggle()
            }){
             Text("name")
            }
            Button(action: {
                self.selectedUser = user.name
                self.sheetType = .edit
                self.showingDetail.toggle()
            }){
             Text("edit")
            }
            Button(action: {
                self.selectedUser = user.name
                self.sheetType = .yourAnyChoice
                self.showingDetail.toggle()
            }){
             Text("yourAction")
            }
        }
   }
   .sheet(isPresented: self.$showingDetail){
   detailView(text:self.$selectedUser,type:self.$sheetType)
   }
  }

struct detailView:View {
  @Binding var text:String
  @Binding var type:SheetType
  var body:some View{
    if type == SheetType.preview{
      Text(text)
    }
    if type == .edit {
       yourEditingView() // as per your requirements 
     }
    if type == SheetType.yourAnyChoice{
      yourChoiceViews()
        }
    
}
Mir
  • 411
  • 1
  • 7
  • 16
  • 1
    I can present more than one sheet in swiftUI but I couldn't pass to data. You can check here. [Multiple Sheet!](https://stackoverflow.com/a/58837261/14139338) – Robert Sep 26 '20 at 17:31
  • Did you test it ? it pass data to sheet when you tap on row – Mir Sep 26 '20 at 17:34
  • Yes, it doesn't work. I created to variable and try to pass in. sheet shows only first index in list. – Robert Sep 26 '20 at 17:37
  • Thank you, but I would like you to read the question again. The above code is already working for me. Besides, I want to run 2 different sheets. – Robert Sep 26 '20 at 18:18
  • what do meant by 2 different sheet? differ by appearance, like background color, text color etc, the sheet already displaying data of the row of user tap, that's different by row – Mir Sep 26 '20 at 18:20
  • I need to present 3 sheet. 1 cellDetail - 2 contextmenu(edit cell) - 3 present new view. 2 view need pass data – Robert Sep 26 '20 at 18:32