Gieta Laksmana
  • Blog
  • Profile
  • Projects
    • VR World Arcade
    • Punch For Fun
    • Spectrum
    • Dimension Battle
  • Resume

NSPopUpButton shenanigans

7/8/2018

0 Comments

 
​action/value bound to NSPopupButton is only triggered when user changes the value from the UI. It doesnt get triggered when you change the value programmatically.

For example, if we bind the NSPopupButton's selected index property:
​
Picture
internal var selectedIndex: AnyObject! {
    didSet {
        let oldIndex = oldValue as? Int
        let newIndex = selectedIndex as? Int
        print("selectedIndex didSet, old: \(oldIndex), new: \(newIndex)")
    }
}
changing the selected value via the UI will trigger selectedIndex.didSet as expected
Picture
However, when we change the index programmatically such as this:
popUpButton.selectItem(at: 2)
Doing so will change the value in the UI, but it won't trigger selectedIndex.didSet as expected.

A bigger issue happens when we have this scenario:
  • ​User selects index 1 from the UI
  • We programmatically call popupButton.selectItem(at: 2), the UI changed to show that index 2 is selected
  • User reselects index 1 from the UI
  • selectedIndex.didset is not called for the user action!!
       
This happens because when we change the selected item programmatically to 2, selectedIndex's value doesn't actually change, it stayed at 1. So when the user change the index back to 1 via the UI, Cocoa realized that the value of selectedIndex is still the same (1), so it decided not to call the didset function.
0 Comments



Leave a Reply.

    Archives

    September 2018
    August 2018
    July 2018
    September 2017
    July 2017
    November 2016
    October 2016
    September 2016
    June 2016
    May 2016
    March 2016

    Categories

    All
    Shenanigans
    Software Engineering
    Swift
    Vr
    Xcode

    RSS Feed

Powered by Create your own unique website with customizable templates.