Post

Replies

Boosts

Views

Activity

Thread 1: Fatal error: No ObservableObject of type msgDatas found. A View.environmentObject(_:) for msgDatas may be missing as an ancestor of this view.
It throw fatal error for data in "  self.data.selectedData = i", even I try to calling @State for solve, but nothing change. Main: struct Main : View {       @EnvironmentObject var data : msgDatas   @State var show: Bool = false       var body : some View{           List(msgs){i in               cellView(pic: i.pic, name: i.name, msg: i.msg, time: i.time, msgs: i.msgs).onTapGesture {                   self.data.selectedData = i         self.show.toggle()       }     }   } } cellView: struct cellView : View {       var pic : String   var name : String   var msg : String   var time : String   var msgs : String       var body : some View{           HStack(spacing: 15){               Image(pic).resizable().frame(width: 50, height: 50).clipShape(Circle())               VStack(alignment:.leading,spacing: 5){                   Text(name)         Text(msg).lineLimit(2)       }               Spacer()               VStack(spacing: 10){                   Text(time)         if msgs != ""{                       Text(msgs).padding(8).background(Color("bg")).foregroundColor(.white).clipShape(Circle())         }         else{                       Spacer()         }       }             }.padding(9)   } }
30
0
3.6k
Mar ’21
Click circle image and navigate new SwiftUI
I have custom search bar and custom circle image in toolbar, when I click the circle image, I want to navigate new SwiftUI. Any idea will be appreciated. ImageView: VStack(alignment: .leading, spacing:0){              HStack(spacing: 0){                 TextField("Search", text: $search)           .padding(.vertical,5)           .padding(.horizontal)           .background(Color.gray.opacity(0.090))           .cornerRadius(30)           .frame(width: 330, height: 32)                  Image("imgage")           .resizable()           .aspectRatio(contentMode: .fill)           .frame(width: 32, height: 32)           .clipShape(Circle())           .overlay(Circle().stroke(Color("fig"), lineWidth: 1))               } } ContentView: var body: some View {    //I have tabview here// }
9
0
2.7k
Mar ’21
when click list item, open new SwiftUI for every list items in SwiftUI
I have a simple SwiftUI project, when I click the any list items, I want to route different SwiftUI. Below, I used arrays, and for every SwiftUI, I want to use [0], [1],..., but it throw error, I do not know why? Any idea? ContentView: import SwiftUI struct ContentView: View {   var body: some View {     NavigationView {       List(contacts) { contact in         NavigationLink(destination: NumberOneView(contact: contact)) {           ContactRow(contact: contact)         }       }       .navigationBarTitle("Contacts")     }     .environment(\.colorScheme, .light)   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } } struct ContactRow: View {       let contact: Settings       var body: some View {     HStack {       Image(contact.imageName)         .resizable()         .aspectRatio(contentMode: .fill)         .frame(width: 20, height: 20)                          VStack(alignment: .leading) {         Text(contact.name)           .font(.system(size: 21, weight: .medium, design: .default))                }     }   } } Settings.swift: import Foundation import SwiftUI struct Settings: Identifiable { let imageName: String let name: String let id = UUID() } let contacts = [ Settings(imageName: "image1", name: "NumberOne"), Settings(imageName: "image2", name: "NumberTwo"), Settings(imageName: "image3", name: "NumberThree"), Settings(imageName: "image4", name: "NumberFour"), ] NumberOneView: import SwiftUI struct NumberOneView: View { let contact: Settings var body: some View { Text("hey") } } struct NumberOneView_Previews: PreviewProvider { static var previews: some View { NumberOneView(contact: contacts[0]) } } NumberTwoView: import SwiftUI struct NumberTwoView: View { let contact: Settings var body: some View { Text("hey") } } struct NumberTwoView_Previews: PreviewProvider { static var previews: some View { NumberTwoView(contact: contacts[1]) } }
6
0
4.2k
Mar ’21
How can I handle NavigationLink for HStack row
I want to use NavigationLink for open the chat detail view when I click the rows items. Here example of code I mentioned. Any idea will be appreciated. RecentRowView: import SwiftUI struct RecentRowView: View { var recent: Profile var animation: Namespace.ID // Environment Object... @EnvironmentObject var profileData: ProfileDetailModel var body: some View { HStack(spacing: 15){ // Making it as clickable Button.... Button(action: { withAnimation{ profileData.selectedProfile = recent profileData.showProfile.toggle() } }, label: { ZStack{ // Without matched geometry effect simply showing image... Image(recent.profile) .resizable() .aspectRatio(contentMode: .fill) .frame(width: 60, height: 60) .clipShape(Circle()) if !profileData.showProfile{ Image(recent.profile) .resizable() .aspectRatio(contentMode: .fill) // Matched Geometry Effect... // Giving unique ID that is from UUID from profile Model.... .matchedGeometryEffect(id: recent.id, in: animation) .frame(width: 60, height: 60) .clipShape(Circle()) } } }) // it decreased the highlight color.... .buttonStyle(PlainButtonStyle()) VStack{   NavigationLink(destination: ChatDetailView(), isActive: $profileData.show) { HStack{ VStack(alignment: .leading, spacing: 8, content: { Text(recent.userName) .fontWeight(.bold) Text(recent.lastMsg) .font(.caption) .foregroundColor(.gray) }) Spacer(minLength: 10) Text(recent.time) .font(.caption2) .foregroundColor(.gray) } Divider() } } } .padding(.horizontal) } } struct RecentRowView_Previews: PreviewProvider { static var previews: some View { ContentView() } } ContentView: struct ContentView: View { // ANimation Namespace... @Namespace var animation // StateObject... @StateObject var profileData = ProfileDetailModel() var body: some View { Home(animation: animation) // setting Environment Object... .environmentObject(profileData) } } struct ContentView_Previews: PreviewProvider { static var previews: some View {     NavigationView{ ContentView() } } } ChatDetailView: import SwiftUI struct ChatDetailView: View {   var body: some View {     Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)   } } struct ChatDetailView_Previews: PreviewProvider {   static var previews: some View {     ChatDetailView()   } }
6
0
2.1k
Jun ’21
Why list menu items not shown in SwiftUI project?
I have list item menu project in SwiftUI, my project work, but I have list icons, they are not shown in list view, even I was try to use system image, there is not any change on screen, any idea? struct MasterView: View { let view1 = Menu(name:"Home", image:"image_home", destination:.home) let view2 = Menu(name:"View 1", image:"image_view1", destination:.view1) let view3 = Menu(name:"View 3", image:"image_view2", destination:.view2) var body: some View { let menus: [Menu] = [view1, view2, view3] return List { ForEach(menus) { menu in self.destinationView(menu: menu) }}} func destinationView(menu: Menu) - some View { switch menu.destination { case .view1: return NavigationLink( destination: AnyView(View1(menu: menu)) ) { Text("\(menu.name)") } case .view2: return NavigationLink( destination: AnyView(View2(menu: menu)) ) { Text("\(menu.name)") } default: return NavigationLink( destination: AnyView(HomeView(menu: menu)) ) { Text("\(menu.name)") }}}} Model.swift: /// Main menu item for the list struct Menu: Identifiable { var id = UUID() var name: String var image: String var destination: ViewType } enum ViewType { case home case view1 case view2 }
5
0
1.6k
Apr ’21
Value of type 'Text' has no member 'searchable'
I want to use searchbar for name, but it is throw an arror like " Value of type 'Text' has no member 'searchable'" any idea?   @State private var searchText = ""    Text(data.name)                 .font(.custom("Helvetica Neue", size: 14))                 .searchable(text: $searchText)
4
0
2.8k
Aug ’21
Trailing closure passed to parameter of type 'Int' that does not accept a closure
I have TabView in ContentView and I want to add TabView for OnboardingView in OtherView, every things work, but it is throw error for TabView in OtherView like "Trailing closure passed to parameter of type 'Int' that does not accept a closure" I do not know why? Any idea? ContentView: struct TabView : View {       var body: some View{           VStack(spacing: 0){ ....... } OtherView:    VStack {     TabView {       ForEach(onboardingData) { onboardingItem in          OnboardingCard(onboardingItem: onboardingItem)       }   }   .tabViewStyle(PageTabViewStyle(indexDisplayMode: .automatic))   .indexViewStyle(PageIndexViewStyle (backgroundDisplayMode:   .always))   .foregroundColor(.white) }
4
1
6.6k
Aug ’21
why HStack items not alignnment in SwiftUI?
I have simple project in swiftUI, everythings is work, but HStack for  Rectangle() as I mention below, do not alignment the Rectangle and text at the same line, and idea will be appreciated. struct App: View {   var body: some View {         GeometryReader{g in              ZStack {         ForEach(0..data.count) { i in           DrawShape(center: CGPoint(x:g.frame(in: .global).width/2, y: g.frame(in: .global).height/2), index: i)         }              }     }     .frame(height: 200)          .clipShape(Circle())     .shadow(radius: 10)         VStack{       ForEach(data) { i in         HStack {                     Text(i.name)             .frame(width:100)             .padding()                      GeometryReader { g in             HStack{               Spacer(minLength: 0)               Rectangle()                 .fill(i.color)                 .frame(width: self.getWidth(width: g.frame(in: .global).width, value: i.percent) ,height: 10)                                              Text(String(format: "\(i.percent)", "%.0f"))                 .fontWeight(.bold)                 .padding(.leading,10)                 .frame(width:80)                Spacer()                                            }.frame(width: 240, height: 30)                                      }                     }                 }       .padding()       Spacer()     }                       }   func getWidth(width: CGFloat, value: CGFloat) - CGFloat {     let temp = value / 100     return temp * width   } }
3
0
3.3k
May ’21
How we can use userInteractionEnabled in (number.name) for number only?
I have number.name array like 1.go, 2.java, 3.swift etc..and I do not want to edit the number again, user will only change name. Is it possible?   if words.count == 24 {         for (index, textField) in textFields.enumerated() {           textField.delegate = self           textField.firstDesign()           textField.text = "\(index + 1). \(words[index])"           mneArr.append(words[index])         }
3
0
495
Jul ’21
How can I use the searchable feature for my row data in SwiftUI?
I have data in row like  Text(data.user) and I want to use searchable for it, I can use search bar, bat I want to use filter for user.    Text(data.user)                   .searchable(text: $searchText, placement: .navigationBarDrawer) like here, tere is some example for filter, but how can I use it for my data. Any idea? .onChange(of: searchText) { searchText in       if !searchText.isEmpty {         articles = sampleArticles.filter { $0.title.contains(searchText) }     } else {         articles = sampleArticles     } }
3
0
553
Sep ’21
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
2k
Feb ’22
How can I use multiple `.alert` dialog in SwiftUI?
I have multiple alert dialog in project, just one of them is work, but I am still do not know why I am not use second one, so how we can use multiple .alert dialog in SwiftUI? struct MyView: View { @State private var selectedUsers: MyModel?   @State var datas: [MyModel]    @State private var deleteRow = false   @State private var deleteRows = false var body: some View { ScrollView(.vertical, showsIndicators: false, content: { VStack(content: { ForEach(datas){ data in MyRowView(data: data) .contextMenu { Button(action: {     deleteRow = true }) { Text("delete") } Button(action: {    deleteRows = true }) { Text("delete") } } .onTapGesture { selectedUsers = data }   .alert(isPresented: $deleteRow) {               Alert(title: Text("title"),                 message: Text("message"),                 primaryButton: .destructive(Text("Delete")) {                                 self.delete(item: data)                                   },                 secondaryButton: .cancel())             }   .alert(isPresented: $deleteRows) {               Alert(title: Text("title"),                 message: Text("message"),                 primaryButton: .destructive(Text("Delete")) {                    self.datas.removeAll()                 },                 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) } }}
3
1
2.2k
Mar ’22
Thread 1: Fatal error: No ObservableObject of type msgDatas found. A View.environmentObject(_:) for msgDatas may be missing as an ancestor of this view.
I have simple app, when I run my project it work, but then throw error after running like "Thread 1: Fatal error: No ObservableObject of type msgDatas found. A View.environmentObject(_:) for msgDatas may be missing as an ancestor of this view." struct Home: View {   @EnvironmentObject var data : msgDatas       var body : some View{           ZStack{               Color("bg").edgesIgnoringSafeArea(.top)               NavigationLink(destination: Message(), isActive: $data.show) {                 Text("")       }       VStack{                   topView()       }     }   } }
2
0
1.9k
Mar ’21