I am trying to pass core data down to a sub/child view but I can't get it to work.
In my SingleView, I have:
struct CitySingleView: View {
#Environment(\.managedObjectContext) var managedObjectContext
var city: Cities
var body: some View {
VStack {
HeaderView().environment(\.managedObjectContext, managedObjectContext)
Text("\(city.name ?? "no name")") // <--- This works and prints the name
}
}
}
in my HeaderView, I have:
struct HeaderView: View {
#Environment(\.managedObjectContext) var managedObjectContext
var city: Cities?
.......
var body: some View {
Text("\(city?.name ?? "Not found")").font(.system(size: 36)) // <--- This doesn't work and prints "not found"
}
}
Do I have to pass the current city as an argument to the HeaderView, and if yes, how can I do this?
so, turns out I the argument I need to pass is city: city.... ;-)
Related
Starting with this
var body: some View {
ScrollView {
VStack(spacing: 0.0) {
Some views here
}
}
.edgesIgnoringSafeArea(.top)
}
How would I add
List(suggestions, rowContent: { text in
NavigationLink(destination: ResultsPullerView(searchText: text)) {
Text(text)
}
})
.searchable(text: $searchText)
on top if that scrollable content?
Cause no matter how I hoax this together when
#State private var suggestions: [String] = []
gets populated (non empty) the search results are not squeezed in (or, better yet, shown on top of
"Some views here"
So what I want to achieve in different terms: search field is on top, scrollable content driven by the search results is underneath, drop down with search suggestions either temporarily squeeses scrollable content down or is overlaid on top like a modal sheet.
Thanks!
If you are looking for UIKit like search behaviour you have to display your results in an overlay:
1. Let's declare a screen to display the results:
struct SearchResultsScreen: View {
#Environment(\.isSearching) private var isSearching
var results: [String]?
var body: some View {
if isSearching, let results {
if results.isEmpty {
Text("nothing to see here")
} else {
List(results, id: \.self) { fruit in
NavigationLink(destination: Text(fruit)) {
Text(fruit)
}
}
}
}
}
}
2. Let's have an ObservableObject to handle the logic:
class Search: ObservableObject {
static private let fruit = [
"Apples 🍏",
"Cherries 🍒",
"Pears 🍐",
"Oranges 🍊",
"Pineapples 🍍",
"Bananas 🍌"
]
#Published var text: String = ""
var results: [String]? {
if text.isEmpty {
return nil
} else {
return Self.fruit.filter({ $0.contains(text)})
}
}
}
3. And lastly lets declare the main screen where the search bar is displayed:
struct ContentView: View {
#StateObject var search = Search()
var body: some View {
NavigationView {
LinearGradient(colors: [.orange, .red], startPoint: .topLeading, endPoint: .bottomTrailing)
.overlay(SearchResultsScreen(results: search.results))
.searchable(text: $search.text)
.navigationTitle("Find that fruit")
}
}
}
I just cannot figure out how to pass core data values of an item from ForEach list in NavigationLink to Detail view. Here is the code that got error: "Cannot convert value of type 'FetchedResults.Element' (aka 'FileEnt') to expected argument type 'FileViewModel'"
struct FileList: View {
#Environment(\.managedObjectContext) var context
#FetchRequest(entity: FileEnt.entity(), sortDescriptors: [NSSortDescriptor(key: "fileName", ascending: false)]) var results: FetchedResults<FileEnt>
#ObservedObject var fileData : FileViewModel
var body: some View {
NavigationView {
List {
ForEach(results) { aFile in
NavigationLink(destination: FileDetails(fileData: aFile), label: {
// ** error on red-underscored aFile above.
Text(aFile.fileName ?? "")
})
}
}.navigationBarTitle("Files")
}
}
}
FileViewModel is for Add, Edit and Detail views. Here is its simplified version for the question:
class FileViewModel: ObservableObject {
#Published var fileName = ""
init(){
}
func DetailItem(fileItem: FileEnt){
fileName = fileItem.fileName ?? ""
}
}
FileDetails:
struct FileDetails: View {
#ObservedObject var fileData : FileViewModel
#Environment(\.managedObjectContext) var context
#State var isEdit = false
var body: some View {
VStack {
Form {
HStack {
Text("File Name:")
Spacer()
Text(fileData.fileName)
....
Modified the post for easy understanding. Thanks for any advice.
I have created a list of clients which get saved in core data. I added some attributes through an AddView and now I am trying to add a new attribute using a different view. In order to do that I understood I need to use the observedObject property wrapper so that it doesn't create a new client but update the existing one. This is the code:
import SwiftUI
import CoreData
struct TopicView: View {
#Environment(\.managedObjectContext) var managedObjectContext
let myStudent: StudentData
#ObservedObject var topic: StudentData
#State var content = ""
#State private var show = false
var body: some View {
Section (header: Text("Topics covered")
.bold()
.padding(.all)
.font(.title3)) {
TextField("Add content", text: $topic.content ?? "") //error here
//Text(topic.content ?? "")
.padding()}
Button ("Save")
{ let newStudent = StudentData(context: self.managedObjectContext)
newStudent.content = self.topic.content
try? self.managedObjectContext.save()
self.topic.content = ""
}.foregroundColor(.blue)
.cornerRadius(10)
.frame(width: 200, height: 50, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.font(.headline)
Spacer()
}
}
I get two errors saying:
Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding'
and
Cannot convert value of type 'String' to expected argument type 'Binding<String?>'
It might be an easy fix but as I am still learning I can't get my head around it.
Thanks
Thank you all,
This is what I found and seems to work:
TextField("Add content", text: Binding(
get: { self.topic.content ?? ""},
set: { self.topic.content = $0 } )
)
I have a parent view which does a #FetchRequest and passes the FetchedResults<T> to a child view. Everything works, and the child view is able to parse through the FetchedResults. However, I can't figure out how to set up the data so that the child's Preview struct will work. What's the proper way to set up some constant data in Preview struct so that I can instantiate the child view and pass in FetchedResults<T>?
As FetchedResults<T> is a RandomAccessCollection and swift array also is a RandomAccessCollection, here is possible solution.
Update: verified with Xcode 13.3 / iOS 15.4
struct ContentView: View {
#Environment(\.managedObjectContext) var context
#FetchRequest(entity: Person.entity(), sortDescriptors: [])
var result: FetchedResults<Person>
var body: some View {
VStack(alignment: .leading) {
Text("Persons").font(.title)
PersonsView(results: result) // FetchedResults<Person> is a collection
}
}
}
// generalize PersonsView to depend just on collection
struct PersonsView<Results:RandomAccessCollection>: View where Results.Element == Person {
let results: Results
var body: some View {
ForEach(results, id: \.self) { person in
Text("Name: \(person.name ?? "<unknown>")")
}
}
}
// Tested with Xcode 11.4 / iOS 13.4
// DOES NOT WORK ANYMORE !!
// struct ChildView_Previews: PreviewProvider {
// static var previews: some View {
// PersonsView(results: [Person()]) // << use regular array //to test
// }
}
Update: fixed & tested part for Xcode 12 / iSO 14 (due to crash of above PreviewProvider)
It appears entity now should be read & specified explicitly:
struct ChildView_Previews: PreviewProvider {
static let entity = NSManagedObjectModel.mergedModel(from: nil)?.entitiesByName["Person"]
static var previews: some View {
let person = Person(entity: entity!, insertInto: nil)
person.name = "Test Name"
return PersonsView(results: [person])
}
}
Use the preview PersistenceController within your pre-generated PersistenceController struct (inside the "Persistence.swift" file).
so if you pass an item from a Core Data "Item"-entity:
struct ContentView: View {
...
private var items: FetchedResults<Item>
..
ForEach(items) { item in
DetailView(item: item)
}
..
In the Detail-View go like this:
struct DetailView: View {
var item: FetchedResults<Item>.Element
var body: some View {
Text("Items text = \(item.text ?? "")")
}
}
struct Detail_Previews: PreviewProvider {
static var previews: some View {
let viewContext = PersistenceController.preview.container.viewContext
let previewItem = Item(context: viewContext)
previewItem.text = "Text4preview"
return Detail(item: previewItem)
}
}
I'm trying to use TextField to change the data of an attribute of CoreData, and everything I've come up with hasn't been successful. There is a similar question (listed below), and I'm going to post the code from the correct answer to that to explain it.
struct ItemDetail: View {
#EnvironmentObject var itemStore: ItemStore
let idx: Int
var body: some View {
NavigationView {
Stepper(value: $itemStore.items[idx].inventory) {
Text("Inventory is \(self.itemStore.items[idx].inventory)")
}
// Here I would like to do this
// TextField("PlaceHolder", $itemStore.items[idx].name)
// That doesn't work... also tried
// TextField("PlaceHolder", $name) - where name is a #State String
// How can you then automaticlly assign the new value of #State name
// To $itemStore.items[idx].name?
.padding()
.navigationBarTitle(itemStore.items[idx].name)
}
}
}
Original Question:
SwiftUI #Binding doesn't refresh View
I now have it working.
struct ItemDetail: View {
#EnvironmentObject var itemStore: ItemStore
let idx: Int
// Added new #State variable
#State var name = ""
var body: some View {
NavigationView {
Stepper(value: $itemStore.items[idx].inventory) {
Text("Inventory is \(self.itemStore.items[idx].inventory)")
}
TextField("Placeholder", text: $name) {
// When the enter key is tapped, this runs.
self.itemStore.items[self.idx].name = self.name
}
.padding()
.navigationBarTitle(itemStore.items[idx].name)
}
}
}