3

原谅超级菜鸟的问题,但过去一个小时我一直在谷歌上搜索这个问题,我很沮丧,因为我似乎无法找到这样一个非常基本的问题的答案:

如何处理 Cocoa 中的控制更改?

我来自 iOS,很明显 Cocoa 不像 UIKit 那样使用插座或委托来处理事件,我觉得我只是在这里遗漏了一些非常重要的信息。我发现它使用了第一响应者链,但除此之外,我无法弄清楚如何实际执行它,或者如何找到这些事件的定义或记录位置。

所以我有一个NSSegmentedControl内部 a NSToolBar,我只想知道用户何时更改所选段。我仔细阅读了类文档,但没有看到任何要处理的事件或动作的提及。我确实注意到,如果我将控件的action出口拖到我的第一响应者代理上,我会列出数万亿个不同的操作,但这些操作似乎都不相关。

我到底如何在 Cocoa 中做到这一点?

4

1 回答 1

5

这是我的一个项目的代码

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)")
}
于 2017-05-07T03:48:23.440 回答