button state behaving wrong ?

Using an IBAction ...(_ sender: NSButton) getting the state to change a bool. However setting the state initially whilst populating a table gives oposite results ...

@IBAction func addCheckValue(_ sender: NSButton) {
    let object_row = tableView.row(for: sender)
    lt state = sender.state

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
...
if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "identCellAdd"), owner: nil) as? NSTableCellView {
    if let button = (cell.subviews[0] as? NSButton) {
        button.state = file.add ? NSControl.StateValue.on : NSControl.StateValue.off
    }
    return cell

If the state is on the action's state will be normal 1 on 0 off, but if it is off 1 becomes off and 0 on.

Replies

Hard to analyze, because we do not see all the values (what is the value of file.add ?)


However setting the state initially whilst populating a table gives oposite results ...


So, it is not whilst firing addCheckValue ?

Why did you expect a different value ? It depends on file.add


Sometimes, what happen is that button states changes on click, and when entering IBAction, state has already changed. But that does not seem to be the issue here in the use case you describe.

"file.add" is only a bool. The state in addCheckValue()) is where the problem is noticed, there's really nothing else to show, just for some reason setting the button state with NSControl.StateValue on or off reverses the value.


if let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "identCellAdd"), owner: nil) as? NSTableCellView {  
    if let button = (cell.subviews[0] as? NSButton)

Curious, is this the right way to work with the button ?

Sorry, should have asked before.


What do you mean:

If the state is on the action's state will be normal 1 on 0 off, but if it is off 1 becomes off and 0 on.


I do not see where you test for 0 or 1

The test is just a print from the addCheckValue func sender.state.


If the button state is set "NSControl.StateValue.on" the return state is 1:0 as normal but "NSControl.StateValue.off" is still 1:0 so off is now 1 and on is 0.

I would really appreciate to test the complete code here, to understand what may happen, as it is really obscure.