NSTokenField and deleting tokens

前端 未结 2 1644
粉色の甜心
粉色の甜心 2020-12-18 08:48

My app allows users to attach tags to certain model objects (subclasses of NSManagedObject). The Tag class is also a subclass of NSManagedObject. I decided to use NSTo

2条回答
  •  北海茫月
    2020-12-18 09:34

    I gave up (after stumbling around for more than 6 hours) on the approach of in place editing my tags using NSTokenField. I eventually ended up with a number of fragile hacks which would ripple through my application as this feature is needed in various places.

    Unless somebody has some strong points to counter my current opinion, NSTokenField is a bit of an ugly monster bringing a half baked implementation to the party. Which is a shame as the presentation side of it really appeals to me...

    EDIT: After some further experimenting, I settled on a reasonably acceptable compromise. I use NSTokenField in a readonly mode. It takes the relevant tags from my Core Data store and displays them as tokens. I added a menu to each token which allows the user to edit, delete or review a tag. A standard push button next to the token field allows to add a new tag. Editing and reviewing is implemented using NSPopovers. See this example:

    enter image description here

    There are still some minor issues:

    • The tokens tend to disappear at arbitrary times when hovering the mouse over the token field. This appears to be a bug.
    • As the token field only accepts NSArray for its binding, I introduced a "virtual property" named tagsAsArray that takes the associated tags and converts them from NSSet to NSArray. I think this impacts the KVO as edits of tags only show up after pressing enter or clicking outside the token field.

提交回复
热议问题