Post

Replies

Boosts

Views

Activity

SwiftUI sidebar within NSHostedView on macOS doesn't show selections
I have an AppKit app written mostly in Objective-C. I'm trying to convert my NSOutlineView to a SwiftUI OutlineGroup. Right now I'm just using List{} because I can't get OutlineGroup to work with CoreData. But when I display my list of items in the sidebar, I can't select any items. I can click on them of course, but the row selection highlighting doesn't work at all. I think maybe this might have something to do with the fact that my SwiftUI view is embedded within an NSHostingView. I've setup my NSHostingView like this: import Cocoa import SwiftUI @objc class FormsListHostingController: NSViewController { public var viewModel : FormsListUIView.ViewModel? var formsListView : FormsListUIView? @objc public var databaseDocument : TFDatabaseDocument? override func viewDidLoad() {         super.viewDidLoad() var hostingView : NSHostingView<FormsListUIView>? if let context = self.databaseDocument?.dataAccessManager?.persistentContainer.viewContext { let viewModel = FormsListUIView.ViewModel(managedObjectContext: context) let contentView : FormsListUIView = FormsListUIView(viewModel: viewModel) hostingView = NSHostingView(rootView: contentView) self.view = hostingView! } } } and my SwiftUI view is setup like this: struct FormsListUIView: View { @Environment(\.managedObjectContext) var context @ObservedObject var viewModel: ViewModel @State private var selection: CDForm? = nil @State private var isSelected : Bool = false var body: some View { List(selection: self.$selection) { ForEach(viewModel.categories, id: \.uuid) { (category : CDCategory) in Section(header: Text(category.name ?? "").font(.headline)) { ForEach(Array(category.forms as! Set<CDForm>), id: \.uuid) { (form : CDForm) in FormRow(form: form).onTapGesture { self.selection = form isSelected.toggle() } } } } } .listStyle(SidebarListStyle()) .frame(maxWidth: .infinity, maxHeight: .infinity) } } Any ideas why the row selection doesn't show? I'm suspecting it's a problem with showing a SwiftUI view within an NSHostingView.
1
0
1.1k
Apr ’22
SwiftUI sidebar within NSHostedView on macOS doesn't show selections
I have an AppKit app written mostly in Objective-C. I'm trying to convert my NSOutlineView to a SwiftUI OutlineGroup. Right now I'm just using List{} because I can't get OutlineGroup to work with CoreData. But when I display my list of items in the sidebar, I can't select any items. I can click on them of course, but the row selection highlighting doesn't work at all. I think maybe this might have something to do with the fact that my SwiftUI view is embedded within an NSHostingView. I've setup my NSHostingView like this: import Cocoa import SwiftUI @objc class FormsListHostingController: NSViewController { public var viewModel : FormsListUIView.ViewModel? var formsListView : FormsListUIView? @objc public var databaseDocument : TFDatabaseDocument? override func viewDidLoad() {         super.viewDidLoad() var hostingView : NSHostingView<FormsListUIView>? if let context = self.databaseDocument?.dataAccessManager?.persistentContainer.viewContext { let viewModel = FormsListUIView.ViewModel(managedObjectContext: context) let contentView : FormsListUIView = FormsListUIView(viewModel: viewModel) hostingView = NSHostingView(rootView: contentView) self.view = hostingView! } } } and my SwiftUI view is setup like this: struct FormsListUIView: View { @Environment(\.managedObjectContext) var context @ObservedObject var viewModel: ViewModel @State private var selection: CDForm? = nil @State private var isSelected : Bool = false var body: some View { List(selection: self.$selection) { ForEach(viewModel.categories, id: \.uuid) { (category : CDCategory) in Section(header: Text(category.name ?? "").font(.headline)) { ForEach(Array(category.forms as! Set<CDForm>), id: \.uuid) { (form : CDForm) in FormRow(form: form).onTapGesture { self.selection = form isSelected.toggle() } } } } } .listStyle(SidebarListStyle()) .frame(maxWidth: .infinity, maxHeight: .infinity) } } Any ideas why the row selection doesn't show? I'm suspecting it's a problem with showing a SwiftUI view within an NSHostingView.
Replies
1
Boosts
0
Views
1.1k
Activity
Apr ’22