Skip to main content
edited title
Link
zephyr
  • 418
  • 5
  • 9

Multiple sheet(item: ) triggered by SwipeActions button and ToolBar buttons gets nil object in the first time in SwiftUI

deleted 381 characters in body
Source Link
zephyr
  • 418
  • 5
  • 9

struct LanguagesView: View {

    @State var activeSheet: ActiveSheet? 
@State var toBeEdit: MeLanguage?
    @State  
var toBeEditbody: MeLanguage?some View {
    NavigationView {
        List {
            ForEach(self.meLanguages, id: \.id) { lan in
           HStack {
                      Text("\(lan.wrappedName)")
                    HStack {
  .font(.headline)                      Text("\(lan.wrappedName)")
          
                   }.font(.headline)
                    }.swipeActions(allowsFullSwipe: false) 
                   {
                        Button(
                            action: {                                    
                                     self.toBeEditactiveSheet = lan.editLanguage
                                     self.activeSheettoBeEdit = .editLanguage                                    lan
                                    },
                            label: { Label("Edit", systemImage: "pencil") }
                            ) .tint(.indigo)
                  }
  }
    .sheet(item: $activeSheet,
       }
        }
    onDismiss: {
   .sheet(item: $activeSheet,
            self.toBeEdit = nil onDismiss: {
          
   self.toBeEdit = nil
        }
             ){
                 item in
                 switch item {
                 case .addLanguage:
                     AddLanguage()                     
                 case .sortLanguages:
                     SortLanguagesView()
                 case .editLanguage:
                     if self.toBeEdit != nil {
                         EditLanguageView( meLanguage: self.toBeEdit! )                     
                     }
                     else {
                         Text("self.toBeEdit is nil")
                     }
                     
                 default:
                     Text("No such button on ContentView.")
                 }
             }
    .toolbar {       
        .toolbar {
                    ToolbarItemGroup {
                        HStack {
                            Text("\(self.meLanguages.count) Languages on Card").font(.headline)
                            self.barButtons
                        }
            }
        }
    }
     }
var barButtons: some View {
            HStack {            
         
        Button(
                    action: {
                            self.activeSheet = .sortLanguages
                    },                
                    label: { 
                           Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
                           }
                     ).id("sortLanguages")
                
                Button(
                    action: {
                        self.activeSheet = .addLanguage
                    },
                    label: { Label("Add Language",
                        systemImage: "plus")
                            .imageScale(.large)
                    }
                )
            }
}

}

    @State var activeSheet: ActiveSheet?    
    @State var toBeEdit: MeLanguage?
    
    List {
        ForEach(self.meLanguages, id: \.id) { lan in
           HStack {
                      Text("\(lan.wrappedName)")
                       .font(.headline)                                
                   }
                  .swipeActions(allowsFullSwipe: false) 
                   {
                       Button(
                            action: {                                    
                                     self.toBeEdit = lan
                                     self.activeSheet = .editLanguage                                    
                                    },
                            label: { Label("Edit", systemImage: "pencil") }
                            ) .tint(.indigo)
                  }
}
    .sheet(item: $activeSheet,
                   onDismiss: {
                self.toBeEdit = nil            
             }
             ){
                 item in
                 switch item {
                 case .addLanguage:
                     AddLanguage()                     
                 case .sortLanguages:
                     SortLanguagesView()
                 case .editLanguage:
                     if self.toBeEdit != nil {
                         EditLanguageView( meLanguage: self.toBeEdit! )                     
                     }
                     else {
                         Text("self.toBeEdit is nil")
                     }
                     
                 default:
                     Text("No such button on ContentView.")
                 }
             }
    .toolbar {               
                    ToolbarItemGroup {
                        HStack {
                            Text("\(self.meLanguages.count) Languages on Card").font(.headline)
                            self.barButtons
                        }
                    }
    
     var barButtons: some View {
            HStack {            
                Button(
                    action: {
                            self.activeSheet = .sortLanguages
                    },                
                    label: { 
                           Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
                           }
                     )
                
                Button(
                    action: {
                        self.activeSheet = .addLanguage
                    },
                    label: { Label("Add Language",
                        systemImage: "plus")
                            .imageScale(.large)
                    }
                )
            }

struct LanguagesView: View {

@State var activeSheet: ActiveSheet?
@State var toBeEdit: MeLanguage?
    
var body: some View {
    NavigationView {
        List {
            ForEach(self.meLanguages, id: \.id) { lan in                   
                    HStack {
                        Text("\(lan.wrappedName)")
                            .font(.headline)
                    }.swipeActions(allowsFullSwipe: false) {
                        Button(
                            action: {
                                self.activeSheet = .editLanguage
                                self.toBeEdit = lan
                            },
                            label: { Label("Edit", systemImage: "pencil") }
                        ) .tint(.indigo)
                    }
            }
        }
        .sheet(item: $activeSheet,
               onDismiss: {
            self.toBeEdit = nil
        }
         ){
             item in
             switch item {
             case .addLanguage:
                 AddLanguage()
             case .sortLanguages:
                 SortLanguagesView()
             case .editLanguage:
                 if self.toBeEdit != nil {
                     EditLanguageView( meLanguage: self.toBeEdit! )
                 }
                 else {
                     Text("self.toBeEdit is nil")
                 }
             default:
                 Text("No such button on ContentView.")
             }
         }
         
        .toolbar {
            ToolbarItemGroup {
                HStack {
                    Text("\(self.meLanguages.count) Languages on Card").font(.headline)
                    self.barButtons
                }
            }
        }
    }
}
var barButtons: some View {
    HStack {
         
        Button(
            action: {
                self.activeSheet = .sortLanguages
            },
            label: { Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
            }
        ).id("sortLanguages")
        
        Button(
            action: {
                self.activeSheet = .addLanguage
            },
            label: { Label("Add Language",
                systemImage: "plus")
                    .imageScale(.large)
            }
        )
    }
}

}

deleted 35 characters in body
Source Link
zephyr
  • 418
  • 5
  • 9

I am using swipeActions in ForEach loop of a List and toolbar buttons to show different sheets on my SwiftUI view. But every first time I swipe left and click the Edit button, the object of that line is nil. If I do the same swipe and click thing again, everything goes well. Anyone else had this kind of bug before? Thank you. Here

Here is the related code:

    @State var activeSheet: ActiveSheet?    
    @State var toBeEdit: MeLanguage?
    
    List {
        ForEach(self.meLanguages, id: \.id) { lan in
           HStack {
                      Text("\(lan.wrappedName)")
                       .font(.headline)
                                 
                  }
       }           .swipeActions(allowsFullSwipe: false) {
                   {
         Button(
              Button(
                            action: {                                    
                                     self.toBeEdit = lan
                                     self.activeSheet = .editLanguage                                    
                                },
    },
                            label: { Label("Edit", systemImage: "pencil") }
                            ) .tint(.indigo)
                  }
}
    .sheet(item: $activeSheet,
                   onDismiss: {
                self.toBeEdit = nil            
            }
             ){
                 item in
                 switch item {
                 case .addLanguage:
                     AddLanguage()                     
                 case .sortLanguages:
                     SortLanguagesView()
                 case .editLanguage:
                     if self.toBeEdit != nil {
                         EditLanguageView( meLanguage: self.toBeEdit! )                     
                     }
                     else {
                         Text("self.toBeEdit is nil")
                     }
                     
                 default:
                     Text("No such button on ContentView.")
                 }
             }
    .toolbar {               
                    ToolbarItemGroup {
                        HStack {
                            Text("\(self.meLanguages.count) Languages on Card").font(.headline)
                            self.barButtons
                        }
                    }
    
     var barButtons: some View {
            HStack {            
                Button(
                    action: {
                            self.activeSheet = .sortLanguages
                    },                
                    label: { 
                           Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
                           }
            ).id("sortLanguages"         )
                
                Button(
                    action: {
                        self.activeSheet = .addLanguage
                    },
                    label: { Label("Add Language",
                        systemImage: "plus")
                            .imageScale(.large)
                    }
                )
            }

I am using swipeActions in ForEach loop of a List and toolbar buttons to show different sheets on my SwiftUI view. But every first time I swipe left and click the Edit button, the object of that line is nil. If I do the same swipe and click thing again, everything goes well. Anyone else had this kind of bug before? Thank you. Here is the related code:

@State var activeSheet: ActiveSheet?    
@State var toBeEdit: MeLanguage?

List {
    ForEach(self.meLanguages, id: \.id) { lan in
       HStack {
                     Text("\(lan.wrappedName)")
                     .font(.headline)
                             
                        }.swipeActions(allowsFullSwipe: false) {
                            Button(
                                action: {                                    
                                    self.toBeEdit = lan
                                    self.activeSheet = .editLanguage                                    
                                },
                                label: { Label("Edit", systemImage: "pencil") }
                            ) .tint(.indigo)
}
.sheet(item: $activeSheet,
               onDismiss: {
            self.toBeEdit = nil            
        }
         ){
             item in
             switch item {
             case .addLanguage:
                 AddLanguage()                     
             case .sortLanguages:
                 SortLanguagesView()
             case .editLanguage:
                 if self.toBeEdit != nil {
                     EditLanguageView( meLanguage: self.toBeEdit! )                     
                 }
                 else {
                     Text("self.toBeEdit is nil")
                 }
                 
             default:
                 Text("No such button on ContentView.")
             }
         }
.toolbar {               
                ToolbarItemGroup {
                    HStack {
                        Text("\(self.meLanguages.count) Languages on Card").font(.headline)
                        self.barButtons
                    }
                }

 var barButtons: some View {
        HStack {            
            Button(
                action: {
                    self.activeSheet = .sortLanguages
                },                
                label: { Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
                }
            ).id("sortLanguages")
            
            Button(
                action: {
                    self.activeSheet = .addLanguage
                },
                label: { Label("Add Language",
                    systemImage: "plus")
                        .imageScale(.large)
                }
            )
        }

I am using swipeActions in ForEach loop of a List and toolbar buttons to show different sheets on my SwiftUI view. But every first time I swipe left and click the Edit button, the object of that line is nil. If I do the same swipe and click thing again, everything goes well. Anyone else had this kind of bug before? Thank you.

Here is the related code:

    @State var activeSheet: ActiveSheet?    
    @State var toBeEdit: MeLanguage?
    
    List {
        ForEach(self.meLanguages, id: \.id) { lan in
           HStack {
                      Text("\(lan.wrappedName)")
                       .font(.headline)                                
                  }
                  .swipeActions(allowsFullSwipe: false) 
                   {
                       Button(
                            action: {                                    
                                     self.toBeEdit = lan
                                     self.activeSheet = .editLanguage                                    
                                    },
                            label: { Label("Edit", systemImage: "pencil") }
                            ) .tint(.indigo)
                  }
}
    .sheet(item: $activeSheet,
                   onDismiss: {
                self.toBeEdit = nil            
            }
             ){
                 item in
                 switch item {
                 case .addLanguage:
                     AddLanguage()                     
                 case .sortLanguages:
                     SortLanguagesView()
                 case .editLanguage:
                     if self.toBeEdit != nil {
                         EditLanguageView( meLanguage: self.toBeEdit! )                     
                     }
                     else {
                         Text("self.toBeEdit is nil")
                     }
                     
                 default:
                     Text("No such button on ContentView.")
                 }
             }
    .toolbar {               
                    ToolbarItemGroup {
                        HStack {
                            Text("\(self.meLanguages.count) Languages on Card").font(.headline)
                            self.barButtons
                        }
                    }
    
     var barButtons: some View {
            HStack {            
                Button(
                    action: {
                            self.activeSheet = .sortLanguages
                    },                
                    label: { 
                           Label("Sort Languages", systemImage: "arrow.up.arrow.down.circle")
                           }
                     )
                
                Button(
                    action: {
                        self.activeSheet = .addLanguage
                    },
                    label: { Label("Add Language",
                        systemImage: "plus")
                            .imageScale(.large)
                    }
                )
            }
deleted 35 characters in body
Source Link
zephyr
  • 418
  • 5
  • 9
Loading
Source Link
zephyr
  • 418
  • 5
  • 9
Loading