Post

Replies

Boosts

Views

Activity

Why SwiftUI `confirmationDialog` delete the wrong item?
I am trying to use confirmationDialog to delete an item in a List. But what happens is that the wrong item gets deleted. Why? Here is my code: struct MyView: View { @State private var selectedUsers: MyModel?   @State var datas: [MyModel]    @State private var confirmDelete = false var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: { self.delete(item: data) }) { Text("delete") } } .onTapGesture { selectedUsers = data }   .alert(isPresented: $confirmDelete) {               Alert(title: Text("title"),                 message: Text("message"),                 primaryButton: .destructive(Text("Delete")) {                                 self.delete(item: data)                                   },                 secondaryButton: .cancel())             }                               } .onDelete { (indexSet) in self.datas.remove(atOffsets: indexSet) }}) })} private func delete(item data: MyModel) { if let index = datas.firstIndex(where: { $0.id == data.id }) { datas.remove(at: index) } }}
1
1
895
Feb ’22
How we can use alert menu before delete list items in SwiftUI?
I have list items in SwiftUI, and when I delete list items, but when I click the delete button, it delete item randomly, I want to delete seleted item, where is the mistake? struct MyView: View { @State private var selectedUsers: MyModel?   @State var datas: [MyModel]    @State private var confirmDelete = false var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: { self.delete(item: data) }) { Text("delete") } } .onTapGesture { selectedUsers = data }   .confirmationDialog( "Are you sure ?",                   isPresented: $confirmDelete,                   titleVisibility: .visible                 ){                                       Button(role: .destructive) {                     self.delete(item: data)                   } label: {                     Text("ok")                                         }                   Button(role: .cancel) {                                         } label: {                     Text("cancel")                                         }                 }             } .onDelete { (indexSet) in self.datas.remove(atOffsets: indexSet) }}) })} private func delete(item data: MyModel) { if let index = datas.firstIndex(where: { $0.id == data.id }) { datas.remove(at: index) } }}
1
1
892
Feb ’22
How we can use alert menu before delete list items in SwiftUI?
I have list items in SwiftUI, and when I delete list items I want to delete after alert menu, like "do want to delete your list items, ""yes" or "no" is it possible? struct MyView: View { @State private var selectedUsers: MyModel? var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: { self.delete(item: data) }) { Text("delete") } } .onTapGesture { selectedUsers = data } } .onDelete { (indexSet) in self.datas.remove(atOffsets: indexSet) }}) })} private func delete(item data: MyModel) { if let index = datas.firstIndex(where: { $0.id == data.id }) { datas.remove(at: index) } }}
2
0
2.2k
Feb ’22
How we can delete all list items in SwiftUI?
I have a simple app in SwiftUI, and I try to delete all list items with context menu , when I click context menu button, I want to remove all items, is it possible? struct MyView: View { @State private var selectedUsers: MyModel? var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: { self.delete(item: data) }) { Text("delete") } } .onTapGesture { selectedUsers = data } } .onDelete { (indexSet) in selectedUsers.remove(atOffsets: indexSet) }}) })} private func delete(item data: MyModel) { if let index = datas.firstIndex(where: { $0.id == data.id }) { datas.remove(at: index) } }} model: struct MyModel: Identifiable, Hashable, Codable { var id = UUID().uuidString var name: String } var datas = [ MyModel(name: "david"), MyModel(name: "marry"), ]
2
0
1.5k
Feb ’22
Why list items not able to delete in SwiftUI?
I have a simple app in SwiftUI, and I try to delete list items in app , project is working, but still list items not able to delete, I do not know what I did not put in my codes, any idea will be appreciated. struct MyView: View { @State private var selectedUsers: MyModel? var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: { self.delete(item: data) }) { Text("delete") } } .onTapGesture { selectedUsers = data } } .onDelete { (indexSet) in selectedUsers.remove(atOffsets: indexSet) }}) })} private func delete(item data: MyModel) { if let index = datas.firstIndex(where: { $0.id == data.id }) { datas.remove(at: index) } }} model: struct MyModel: Identifiable, Hashable, Codable { var id = UUID().uuidString var name: String } var datas = [ MyModel(name: "david"), MyModel(name: "marry"), ]
3
0
1.8k
Feb ’22
why it is throw an error as a Value of type 'ContentView' has no member 'restaurants' in SwiftUI?
I have example of swiftUI project, and it is throw an error as a "Value of type 'ContentView' has no member 'restaurants'" for    self.restaurants.remove(atOffsets: indexSet) } line of code, I do not know what I missed, any idea? import SwiftUI struct ContentView: View {          @State private var selectedRestaurant: Restaurant?       var body: some View {     List {       ForEach(restaurants) { restaurant in         BasicImageRow(restaurant: restaurant)           .contextMenu {                           Button(action: {               // mark the selected restaurant as check-in               self.checkIn(item: restaurant)             }) {               HStack {                 Text("Check-in")                 Image(systemName: "checkmark.seal.fill")               }             }                           Button(action: {               // delete the selected restaurant               self.delete(item: restaurant)             }) {               HStack {                 Text("Delete")                 Image(systemName: "trash")               }             }                           Button(action: {               // mark the selected restaurant as favorite               self.setFavorite(item: restaurant)             }) {               HStack {                 Text("Favorite")                 Image(systemName: "star")               }             }           }           .onTapGesture {             self.selectedRestaurant = restaurant           }           .actionSheet(item: self.$selectedRestaurant) { restaurant in                           ActionSheet(title: Text("What do you want to do"), message: nil, buttons: [                               .default(Text("Mark as Favorite"), action: {                 self.setFavorite(item: restaurant)               }),                               .destructive(Text("Delete"), action: {                 self.delete(item: restaurant)               }),                               .cancel()             ])           }       }       .onDelete { (indexSet) in         self.restaurants.remove(atOffsets: indexSet)       }     }   }       private func delete(item restaurant: Restaurant) {     if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {       self.restaurants.remove(at: index)     }   }       private func setFavorite(item restaurant: Restaurant) {     if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {       self.restaurants[index].isFavorite.toggle()     }   }       private func checkIn(item restaurant: Restaurant) {     if let index = self.restaurants.firstIndex(where: { $0.id == restaurant.id }) {       self.restaurants[index].isCheckIn.toggle()     }   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } struct BasicImageRow: View {   var restaurant: Restaurant       var body: some View {     HStack {       Image(restaurant.image)         .resizable()         .frame(width: 40, height: 40)         .cornerRadius(5)       Text(restaurant.name)               if restaurant.isCheckIn {         Image(systemName: "checkmark.seal.fill")           .foregroundColor(.red)       }               if restaurant.isFavorite {         Spacer()                   Image(systemName: "star.fill")           .foregroundColor(.yellow)       }     }   } } model: struct Restaurant: Identifiable {   var id = UUID()   var name: String   var image: String   var isFavorite: Bool = false   var isCheckIn: Bool = false } var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"),        Restaurant(name: "Homei", image: "homei"),        Restaurant(name: "Teakha", image: "teakha"),        Restaurant(name: "Cafe Loisl", image: "cafeloisl"), ]
1
0
1.4k
Feb ’22
How can use model in view shortly?
I have small restaurans data in content view, but I want to use this data shortly inside of the homeview, I am typing   to use  @State var restaurants: [ Restaurant] but in other view I have HomeView, it is throw an error like     Cannot convert value of type '[Restaurant].Type' to expected argument type '[Restaurant]' for    HomeView( restaurants: [Restaurant]) line of code, I do not know what I missed? Any idea? struct HomeView: View { @State var restaurants = [ Restaurant(name: "Cafe Deadend", image: "cafedeadend"), Restaurant(name: "Homei", image: "homei"), Restaurant(name: "Teakha", image: "teakha"), Restaurant(name: "Cafe Loisl", image: "cafeloisl"), ] }
0
0
288
Feb ’22
How To Show A SwiftUI Onboarding Screen Only When To App Launches For The First Time
I want to use onboarding screen in my project, and it is work but I want to use it just once time for app, I do not know how I will do it, is there any way? struct ContentView: View {   @State private var onboardinDone = false   var data = OnboardingData.data       var body: some View {     Group {       if !onboardinDone {         OnboardingView(data: data, doneFunction: {                              print("done onboarding")         })       } else {         MainScreen()       }     }   }        } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
2
1
1.8k
Feb ’22
How can I add costom emoji to example chat app?
I have simple chat app, and I want to use simple emoji in this chat app, my emoji is located in EmojiView an when I click the emoji button, I want to add in live chat, but I do not know how I will forward for it, I was look at many resources but I did not find any example on internet, is it possible to do it? import SwiftUI struct MessageDetailsView: View {   var body: some View {     HomeMessageDetails()}} struct MessageDetailsView_Previews: PreviewProvider {      static var previews: some View {     MessageDetailsView()      }} struct HomeMessageDetails : View {   @State var message = ""   @StateObject var allMessages = Messages()   @State private var emojiData = false   var body: some View{           ZStack {       VStack{         ScrollView(.vertical, showsIndicators: false, content: {           ScrollViewReader{reader in             VStack{               ForEach(allMessages.messages){message in                 ChatBubble(message: message)               }                .onChange(of: allMessages.messages) { (value) in                 if value.last!.chatMessages{                   reader.scrollTo(value.last?.id)}}}             .padding([.horizontal,.bottom])}})         HStack {             HStack{             TextField("Message", text: self.$message)             Button(action: {             emojiData = true             }, label: {               Image("emoji")             }) .sheet(isPresented: $emojiData) {           EmojiView()         } }           .padding(.vertical, 10)           .padding(.horizontal)                Button(action: {             allMessages.messages.append(Message(id: Date(), message: message, chatMessages: true))                           message = ""           }, label: {             Image("reply")              })   }         .padding(.horizontal)       }} }} struct ChatBubble : View {   var message : Message   var body: some View{     HStack(alignment: .top,spacing: 10){       if message.chatMessages{         Text(message.message)           .foregroundColor(Color("black))         }             else{         Text(message.message)             .foregroundColor(.white)  }}     .id(message.id)}} struct Message : Identifiable,Equatable{   var id : Date   var message : String   var chatMessages : Bool    } class Messages : ObservableObject{   @Published var messages : [Message] = []   init() {           let strings = ["Hii","Hello !!!!"]     for i in 0..<strings.count{       messages.append(Message(id: Date(), message: strings[i], chatMessages: i % 2 == 0 ? true : false))}}           func writeMessage(id: Date,message: String,chatMessages: Bool){           messages.append(Message(id: id, message: message, chatMessages: chatMessages))}} struct EmojiView: View {   var body: some View {  Button(action: {             }, label: {               Image("smile_emoji")                           }) }}
0
0
326
Feb ’22
Why I am not success to pass data from one view to another view for many data in SwiftUI?
I ask this question before, it was answered but it is not work for many data, like when I click the any list items, all details pass name of "david", I want to pass data of user which I clicked, where I missed here? struct ImageModel: Identifiable, Hashable {   var id = UUID().uuidString   var name: String   } var datas = [       ImageModel(name: "davis"),  ImageModel(name: "carry"),   ImageModel(name: "maria"), ] struct ImageRowView: View {   var data: ImageModel   var body: some View {            NavigationLink(destination: ImageDetailsView(data: ImageModel)){                       HStack{} }}} struct ImageDetailsView: View {      var body: some View {          ImageDetails(data:  ImageModel(name: "davis"))   } } struct ImageDetailsView_Previews: PreviewProvider {   static var previews: some View {           ImageDetailsView()         } } struct ImageDetails : View {      var data: ImageModel   var body: some View{   VStack{             Text(data.name)                      } }
1
0
417
Feb ’22
Why I am not success to pass data from one view to another view in SwiftUI?
I have a problem when try to pass data from one view to another view, it is throw an error for   ImageDetails() as a "Missing argument for parameter 'data' in call" I do not know what I missed? struct ImageModel: Identifiable, Hashable {   var id = UUID().uuidString   var name: String   } var datas = [       ImageModel(name: "davis"), ] struct ImageRowView: View {   var data: ImageModel   var body: some View {            NavigationLink(destination: ImageDetailsView(data: ImageModel)){                       HStack{} }}} struct ImageDetailsView: View {      var body: some View {           ImageDetails()   } } struct ImageDetailsView_Previews: PreviewProvider {   static var previews: some View {           ImageDetailsView()         } } struct ImageDetails : View {      var data: ImageModel   var body: some View{   VStack{             Text(data.name)                      } }
1
0
381
Feb ’22
why it throw an error as "'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead"in SwiftUI 3.0?
I have a project, after update it to swiftUI 3.0, it is throw an error for    .animation(.spring()) as animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead, any idea?  @Binding var display: Bool   private var background: some View {   Color.black    .fillParent()    .opacity(0.6)    .animation(.spring())  }
1
0
592
Feb ’22