Post

Replies

Boosts

Views

Activity

Monterey / SwiftUI view not updating as expecting
The following code is stripped out from an app that compiles and works under MacOS 11. It shows a tab bar - clicking on an item will highlight it. Under the tab bar is a text edit field where one can edit the title of the selected tab. There are 2 issues: The tab title shown in the title editor is allows out of phase from the selected tab Irrespective of which tab item is selected, when the title is edited it always updates the first tab I'm not sure if I was very lucky that this code ever worked under Big Sur, or if there is an issue with Monterey. I'm definitely not holding this up as example code - I'm sure there are better ways to implement it, but seek opinions on whether it should work. import SwiftUI class Document: ObservableObject { var tabs = [Tab(id: 0), Tab(id: 1)] var viewSettings = ViewSettings() } class Tab: ObservableObject, Identifiable { let id: Int @Published var title: String init(id: Int) { self.id = id self.title = "Tab \(id)" } } class ViewSettings: ObservableObject { @Published var activeTab: Int = 0 } @main struct Test: App { @StateObject var document: Document = Document() var body: some Scene { WindowGroup { ContentView() .padding() .environmentObject(document) .environmentObject(document.viewSettings) } } } struct ContentView: View { @EnvironmentObject var document: Document @EnvironmentObject var viewSettings: ViewSettings var body: some View { TabBar(viewSettings: document.viewSettings) TabEditView(activeTab: document.tabs[viewSettings.activeTab]) } } struct TabEditView: View { @EnvironmentObject var viewSettings: ViewSettings @ObservedObject var activeTab: Tab @State var title: String = "" init(activeTab: Tab) { print("CONSOLE - Init TabEditView for tab \(activeTab.id)") self.activeTab = activeTab } var body: some View { HStack { Text("Tab title:") TextField("Tab title:", text: $title, onCommit: { activeTab.title = title }) .onAppear { title = activeTab.title } .onChange(of: viewSettings.activeTab) { _ in print("CONSOLE - Updating TextField from tab \(activeTab.id)") title = activeTab.title } } } } struct TabBar: View { @EnvironmentObject var document: Document @ObservedObject var viewSettings: ViewSettings var body: some View { HStack { ForEach(document.tabs, content: TabItem.init) } } } struct TabItem: View { @EnvironmentObject var viewSettings: ViewSettings @ObservedObject var tab: Tab init(_ tab : Tab) { self.tab = tab } var body: some View { Text(tab.title) .padding(2) .background(tab.id == viewSettings.activeTab ? .red : .clear) .cornerRadius(4) .onTapGesture { viewSettings.activeTab = tab.id } } }
0
0
609
Jul ’21
Suppressing verbose console output - SwiftUI / MacOS Monterey
Is there anyway to suppress the very verbose console output I am getting with MacOS Monterey and SwiftUI code? Here's a snippet of the output: 2021-07-20 09:58:18.267602+0100 BBCFTEST[19341:1880374] IMKInputSession (deactivate) CFRunLoopObserver kCFRunLoopExit (State change BEGIN) - innerRunLoopCount=1, otherInnerRunLoopDetected=0 2021-07-20 09:58:18.267637+0100 BBCFTEST[19341:1880374] IMKInputSession (deactivate) CFRunLoopObserver kCFRunLoopExit (State change END) - innerRunLoopCount=0, otherInnerRunLoopDetected=0 2021-07-20 09:58:18.270063+0100 BBCFTEST[19341:1880374] IMKInputSession (deactivate) CFRunLoopRunInMode exited, (kCFRunLoopRunStopped) 2021-07-20 09:58:18.270109+0100 BBCFTEST[19341:1880374] IMKInputSession (deactivate) CFRunLoopRunInMode (kIMKXPCPrivateRunLoopMode) ] RunLoopFinished(1)/Stopped(2) - Run result = 2, (Invocation already done = 1) (Sentinel IsZombie = 0) 2021-07-20 09:58:18.270147+0100 BBCFTEST[19341:1880374] IMKInputSession (deactivate) CFRunLoopRunInMode() LOOP DONE!
7
0
2.3k
Jul ’21
SwiftUI ReferenceFileDocument and Document-scoped bookmarks
Context I'm writing an app that allows a user to select folders, and then scan them for files. The folder could be on the user's machine, iCloud or external storage. The app persists the details of these files and folders to a document, so the user can access the files or re-scan the folders in the future. The App is being written using the SwiftUI framework for MacOS and the iPad. Problems Given this is a Sandboxed app I need to create security-scoped bookmarks to be able to access the files and folders that have been persisted to the document. I have two questions: How can I create a document-scoped bookmark, when using ReferenceFileDocument protocol. I need the document's URL, but I will never have access to this as I'm using the ReferenceFileDocument. I want to achieve something like this:   func fileWrapper(snapshot: MyDocument, configuration: WriteConfiguration) throws - FileWrapper { : let bookmarkDataToPersist = snapshort.sourceFolderURL.bookmarkData(options: .withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: documentURL) : } 2. Ideally the user would be able to: * connect an external drive to their Mac * select a folder on that drive * save the document, which would persist a bookmark to that folder's URL * send the document to an iPad (via email or iCloud drive) * open the document using the iPad version of the App * connect the external bookmark to the iPad * re-scan the folder which was book marked in the document from the folder But given the problem in 1) and the fact that document-bookmarks cannot point to folders, is there a way? Any ideas or suggestions would be very welcome
0
0
564
Apr ’21