1

In my document based application, I have added a Label (NSTextField) to my document's window's titlebar using the new API for 10.10 :

I call this from my main NSViewController that I have controlling the main view that comes with the window for free :

class MasterViewControler: NSViewController {

    @IBOutlet weak var window: NSWindow!

    override func viewDidLoad() {
        super.viewDidLoad()
        window.addTitlebarAccessoryViewController(TitleBarSubviewControler(nibName: "TitleBarSubview", bundle: NSBundle.mainBundle())!)
    }

So that loads the custom view containing my label into my titlebar as the docs suggest. All good so far.

Now at some point I must access that label at runtime to change it's string value, from somewhere within my app.

I tried adding an IBOutlet for the label to the NSTitlebarAccessoryViewController that controls the subview, and as expected from the NSTitlebarAccessoryViewController I can manipulate the labels stringValue. But I can't do much from there.

I tried creating a function in the NSTitlebarAccessoryViewController that sets the label string, and accessing that function via a global instance of the NSTitlebarAccessoryViewController() from my mainViewControler, but although I can access this function and pass it a string, the reference the the labels IBOutlet in the function always unwraps as nil (crash).

So why is the outlet unwrapping as nil in the function when access from anywhere but inside the class where the function resides.?

I must be able to access the label from a controller or view within my windows mainView.

How can I do this..?

My Possible Structure

Jay
  • 6,572
  • 3
  • 37
  • 65
Gary Simpson
  • 2,677
  • 2
  • 17
  • 18

1 Answers1

0

I could not for the life of me find a way to reference the label in the subview I added to the windows titlebar via IB, so the best workaround I can find is this :

class WindowControler: NSWindowController {

    var titlebarLabel:NSTextField?

    override func windowDidLoad() {
        super.windowDidLoad()
        // add the titlebar subview
        window?.addTitlebarAccessoryViewController(TitlebarSubviewControler(nibName: "TitlebarSubview", bundle: NSBundle.mainBundle())!)
        // get reference to the titlibarLabel I just added in the line above
        var subviewsOfTheLastTitleBarView: AnyObject? = window?.standardWindowButton(NSWindowButton.ZoomButton)?.superview?.subviews.last?.subviews[0]
        var titlebarNSTextField: AnyObject? = subviewsOfTheLastTitleBarView?.subviews[0]
        titlebarLabel = (titlebarNSTextField! as NSTextField)
    }

    @IBAction func tempbuttonclicked(sender: AnyObject) {
        titlebarLabel?.stringValue = "Text seen in titlebar subview"
    }

}

So I am hunting through the titlebar here, to find the NSTextField right after I add it to the titlebar in the first line of windowDidLoad, and taking a lot for granted.

I am pretty worried this could break very easily, just not sure about doing it like this. Any better solutions greatly appreciated.

Gary Simpson
  • 2,677
  • 2
  • 17
  • 18