簡體   English   中英

Swift macOS SegmentedControl Action沒有被調用

[英]Swift macOS SegmentedControl Action not getting called

描述

我正在嘗試使用NSSegmentedControls在子ViewController之間過渡。 ParentViewController位於Main.storyboard ,而ChildViewControllers位於Assistant.storyboard 每個ChildViewController都有一個細分控件,該細分控件分為2個細分,它們的主要用途是在ChildViewControllers之間導航。 因此,將它們設置為momentaryPushIn而不是selectOne。 每個ChildViewController使用一個Delegate與ParentViewController進行通信。

因此,在ParentViewController中,我添加了ChildViewControllers,如下所示:

/// The View of the ParentViewController configured as NSVisualEffectView
@IBOutlet var visualEffectView: NSVisualEffectView!

var assistantChilds: [NSViewController] {
    get { return [NSViewController]() }
    set(newValue) {
        for child in newValue { self.addChild(child) }
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
    addAssistantViewControllersToChildrenArray()
}

override func viewWillAppear() {
    visualEffectView.addSubview(self.children[0].view)
    self.children[0].view.frame = self.view.bounds
}

private func addAssistantViewControllersToChildrenArray() -> Void {
    let storyboard = NSStoryboard.init(name: "Assistant", bundle: nil)
    let exampleChild = storyboard.instantiateController(withIdentifier: "ExampleChild") as! ExampleChildViewController
    let exampleSibling = storyboard.instantiateController(withIdentifier: "ExampleSibling") as! ExampleSiblingViewController

    exampleChild.navigationDelegate = self
    exampleSibling.navigationDelegate = self

    assistantChilds = [exampleChild, exampleSibling]
}

到現在為止還挺好。 ExampleChildViewController具有一個NSTextField實例。 當我處於TextField范圍內時,我可以觸發SegmentedControls的操作。 它應該向前和向后導航。 但是,一旦離開TextField的范圍,我仍然可以單擊細分,但它們不會觸發任何操作。 即使TextField不是應用程序的當前“第一響應者”,他們也應該能夠前后導航。 我想我在這里遺漏了一些東西,希望任何人都可以幫助我。 我知道問題不是NSSegmentedControl因為我在NSSegmentedControl看到與NSButton相同的行為,該NSButton被配置為Switch / Checkbox。 我只是不知道我在做什么錯。

這是我第一次在這里自己問問題,所以我希望我的工作方式對解決方案有所幫助。 讓我知道我是否可以做得更好/不同,或者是否需要提供有關某事的更多信息。

提前致謝!


附加信息

為了完整性:

ParentViewController本身嵌入在由RootViewController擁有的ContainerView 我無法想象這確實有關系,但是這樣我們就不會錯過任何東西。


實際上,我沒有顯示導航操作,因為我想使其盡可能簡單。 此外,該動作不是問題,它可以執行我想要的操作。 如果我錯了,請糾正我。


我在研究時發現了可能的解決方案,但不適用於我:


我在研究時發現的相關問題/主題:

let parentViewControllerInstance = self.parent as! ParentViewController
segmentedControl.target = parentViewControllerInstance

就我而言,我只需要將委托設置為sendAction方法的目標sendAction


背景

好的,閱讀了數小時的AppKit文檔后,我現在可以回答我自己的問題了。

首先,調試用戶界面表明問題絕對不在ViewHierarchy中。 調試界面

因此,我嘗試考慮NSButton和NSSegmentedControl的本質。 在某些時候,我注意到這兩個都是NSControl的子類。

class NSSegmentedControl : NSControl
class NSButton : NSControl

AppKit文檔說:

討論區

按鈕是用於在您的應用內啟動操作的標准控件。 您可以將按鈕配置為具有多種不同的視覺樣式,但是行為是相同的。 單擊后,按鈕將調用其關聯的目標對象的操作方法 (...)您可以使用操作方法來執行特定於應用程序的任務。

粗體文本指向解決方案的關鍵– 與其相關的目標對象 通常,我定義一個控制項的操作,如下所示:

button.action = #selector(someFunc(_:))

這將導致NSControl實例調用此方法:

func sendAction(_ action: Selector?, to target: Any?) -> Bool

文檔中的參數說明:

參量

那個行動

在目標上調用的選擇器。 如果選擇器為NULL,則不發送消息。

目標

接收消息的目標對象。 如果對象為nil,則應用程序在響應程序鏈中搜索能夠處理消息的對象 有關調度動作的更多信息,請參見NSActionCell的類描述。

總之,正在觸發操作方法的NSControl實例(在我的情況下為NSSegmentedControl)沒有將其操作發送到的目標。 因此,它只能在響應者鏈中發送其操作方法-當第一個響應者位於另一個視圖中時,該方法顯然為零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM