2

Forgive the super noob question, but I've been googling this for the past hour and am getting frustrated that I can't seem to find an answer to such a very basic question:

How do I handle control changes in Cocoa?

I'm coming from iOS, and it's clear that Cocoa does not use outlets or delegates to handle events in the same way that UIKit does and I feel like I'm just missing some very important piece of information here. I've figured out that it uses the first responder chain, but beyond that I can't figure out how to actually do it, or how to even find where these events are defined or documented.

So I have a NSSegmentedControl inside a NSToolBar and I just want to know when a user changes the selected segment. I've pored through the class documentation but cannot see a single mention of any kind of events or actions to handle. I did notice if I drag the control's action outlet onto my first responder proxy, I get a quadrillion different actions listed, none of which seem relevant in any way.

How on earth do I do this in Cocoa?

devios1
  • 33,997
  • 43
  • 149
  • 241
  • A connected action to an `IBAction` works the same way like in iOS. A very smart way which is not available in iOS is Cocoa Bindings. – vadian Apr 18 '17 at 16:42
  • Where might I find these actions? I looked at the bindings, but it looks like super overkill. I just want a signal or method call or something. – devios1 Apr 18 '17 at 16:46
  • 2
    Create an `IBAction` in the target class and connect the action in Interface Builder (it's the same as in iOS). Bindings are not overkill, far from it. In most cases it's less code. – vadian Apr 18 '17 at 16:48
  • Thanks @vadian, I dragged the `action` outlet into my class and it created a generic action outlet which does indeed get called when the segment changes. I guess I was looking for something a little more specific. – devios1 Apr 18 '17 at 16:56
  • NSSegmentedControl has a `.selectedSegment: Int` property. – mikeD Apr 18 '17 at 20:20
  • Maybe try this: http://stackoverflow.com/questions/24927874/how-to-draw-segmented-control-with-blue-tint-like-xcode/25134440#25134440 – l'L'l Apr 19 '17 at 04:58

1 Answers1

4

This is a code from one of my projects

var currntSeg : Int = 1
@IBOutlet weak var acSwitch: NSSegmentedControl!
@IBAction func SwitchButton(_ sender: AnyObject) {
    switch acSwitch.selectedSegment {
    case 0:
        currntSeg == 0 ?
            self.navVC?.pushViewController(Sleeper!, animated: true) :
            self.navVC?.popViewController(Sleeper!, animated: true)
    case 1:
        currntSeg < 1 ?
            self.navVC?.pushViewController(Work!, animated: true) :
            self.navVC?.popViewController(Work!, animated: true)
    case 2:
        currntSeg < 2 ?
            self.navVC?.pushViewController(Student!, animated: true) :
            self.navVC?.popViewController(Student!, animated: true)
    default:
        self.navVC?.pushViewController(Rose!, animated: true)
    }
    currntSeg = acSwitch.selectedSegment
    print("Selected Seg: \(acSwitch.selectedSegment)")
}
styl3r
  • 501
  • 7
  • 22
  • `acSwitch.selectedSegment` holds the value of the selected segment `SwitchButton(_ sender: AnyObject)` is an IBAction connected to the segment too. – styl3r May 07 '17 at 03:49