Post

Replies

Boosts

Views

Activity

SwiftUI: Menu() doesn't work
Hello Guys, I have a simple code in SwiftUI     Menu { Text("Item") } label: { Text("Menu") } But it's produce an Error like this: "[UILog] Called -[UIContextMenuInteraction updateVisibleMenuWithBlock:] while no context menu is visible. This won't do anything." But this is no warning, in fact, there happens nothing. Has anybody an idea, how to get rid this error message? Thanks in Advance DWD
1
0
2.9k
Mar ’21
UI with FetchRequest dosn’t refresh
Hello Everyone,A short note first, unfortunately the following code is working, I am posting in anyway to show how the whole thing is built up. To make it executable, it still needs a CoreData table named "Person" with three columns (Id = UID, firstname = string, lastname = string).The problem would be here, in the structure "PersonView", not when calling but when changing the detail view.Here the changed record is simply removed from the view instead of displaying it. As long as there is no update, there is no problem.I suppose that the problem in the original is that the @FetchRequest in PersonView is not refreshed. It could also be that the @FetchRequest uses a different managedObjectContext than the one saved.Here are three questions:a) How can you specify an @FetchRequest (see PersonView) which managedObjectContext it should use?b) how could you restart the @FetchRequest when returning a detail view in PersonViewc) how could the functions load() and save() in Object DPerson be made nicer so that it doesn't look so bumpy.As I said, the example unfortunately works, so please see it only as an illustration.Many thanks in advance.import SwiftUI import CoreData class DPerson: ObservableObject { @Published var vorname: String = "" @Published var nachname: String = "" var id: UUID? var ctx: NSManagedObjectContext? func setContext(ctx: NSManagedObjectContext){ self.ctx = ctx } func load(id: UUID){ let request: NSFetchRequest<Person> = Person.fetchRequest() request.predicate = NSPredicate(format: "id = %@", id.description) if let m = try? ctx?.fetch(request), m.count == 1 { self.id = m[0].id self.vorname = m[0].vorname ?? "" self.nachname = m[0].nachname ?? "" } } func save() { let request: NSFetchRequest<Person> = Person.fetchRequest() request.predicate = NSPredicate(format: "id = %@", id!.description) if let m = try? ctx?.fetch(request), m.count == 1 { m[0].vorname = self.vorname m[0].nachname = self.nachname try? ctx?.save() } } } struct PersonDetail: View { @Environment(\.managedObjectContext) var context let personId: UUID? @ObservedObject var dPerson: DPerson = DPerson() var body: some View { List{ TextField("Vorname", text: $dPerson.vorname) TextField("Nachname", text: $dPerson.nachname) }.onAppear(perform: { self.dPerson.setContext(ctx: self.context) self.dPerson.load(id: self.personId!) }) .onDisappear(perform: { self.dPerson.save() }) } } struct ContentView: View { @Environment(\.managedObjectContext) var context struct PersonView: View { var fetchRequest: FetchRequest<Person> var body: some View { List(fetchRequest.wrappedValue, id: \.self) { person in NavigationLink(destination: PersonDetail(personId: person.id) ){ Text("\(person.vorname ?? "") \(person.nachname ?? "")") } } } init() { fetchRequest = FetchRequest<Person>(entity: Person.entity(), sortDescriptors: []) } } var body: some View { VStack{ NavigationView { PersonView() } Spacer() Button(action: { let a = Person(context: self.context) a.id = UUID() a.vorname = "Andy" a.nachname = "Zacherl" let b = Person(context: self.context) b.id = UUID() b.vorname = "Caro" b.nachname = "Smith" let c = Person(context: self.context) c.id = UUID() c.vorname = "Xaver" c.nachname = "Muller" try? self.context.save() }) { Text("Add DB") } } } }
1
0
4.9k
Feb ’20
Calling view and go back
Hello guys,I'm having a little problem again. After creating a new object, I would like to branch to a detaillier on the iPhone without any user action. But after adding the data there should be a manual possibility to come back. Is there a possibility for this? Unfortunately I have not found anything yet.Thanks a lot.The problem is shown in the code below:import SwiftUI class L: ObservableObject { @Published var x = ["a", "b", "c"] } struct ContentView: View { @ObservedObject var l = L() var body: some View { NavigationView { List { ForEach (l.x, id: \.self) {x in NavigationLink(destination: sv()) { Text(x) }.navigationBarTitle(Text("List")) .navigationBarItems(trailing: Button(action: self.addnewvalue, label: { Text("New")})) } } } } func addnewvalue() { l.x.append("d") //how to get automatically into the detailview sv? } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct sv: View { var body: some View { Text("Hello, World!") //how to get back with buttom into the main view? } }
3
0
955
Jan ’20
Navigation View on iPad?
Hi guys,I have a small problem with the following code. It works fine if the device is an iPhone (also simulator). If I use an iPad, the screen remains empty (also in the simulator). Where is the error? I can't find one at the moment.import SwiftUI struct ContentView: View { let l = ["a", "b", "c"] var body: some View { NavigationView { List { ForEach (l, id: \.self) {l in NavigationLink(destination: sv()) { Text(l) }.navigationBarTitle(Text("List")) } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } struct sv: View { var body: some View { Text("Hello, World!") } }
3
0
5.8k
Dec ’19