Creating an outlet for a NSProgressIndicator inside a NSToolbar
Asked Answered
F

1

1

I have this OSX storyboard-based application that starts with a NSSplitViewController like this:

enter image description here

This splitViewController has two viewControllers: master and detail.

Inside the window I have a NSToolbar. I dragged a NSProgressIndicator to that toolbar and Xcode embedded it inside a NSToolbarItem.

Now I need to create an outlet (not an action as explained on other stackoverflow questions) from the NSProgressIndicator to some class. First question is which one?

Xcode will not let I create an outlet. I have tried these options:

  1. dragged from the ToolbarItem to masterController class file, detailController class file and to NSSplitViewController class.
  2. dragged from the ToolbarItem to the delegate class.
  3. dragged from the NSProgressIndicator to masterController class file, detailController class file and to NSSplitViewController class.
  4. dragged from the NSProgressIndicator to the delegate class.
  5. dragged from both the NSToolbarItem and from the NSProgressIndicator to the Window Controller First Responder.

In all cases dragging does not make a window appear to allow me to create the outlet.

For heavens sake, how do I create an outlet like that? To which class I drag it and how do I do that?

Frazer answered 18/12, 2014 at 20:18 Comment(0)
V
5

I'll assume your setup is more like this image:

BasicCocoaAppWithSplitviewAndToolBar

Your Window scene is backed, by default, by an NSWindowController, to which you cannot add new outlets. You need to create a subclass of that, associate it with your Window and then you should be able to create outlets in that.

File > New File > Cocoa Class Specify a name like "SpaceDogsWindowController", as a subclass of NSWindowController.

Then use select the window controller icon (blue circle) and select the Identity Inspector in Xcode. (CMD+ALT+3). Specify the name of your new class in the "Class" field.

Then try to hookup an outlet:

1) Show the Assistant Editor

2) Use the Jump Bar to ensure your custom class is visible (It's at the top of the assistant editor pane, it should say Automatic and you can tap that to then select your new class; If it says 'Manual', change it to Automatic).

3) If your are control-dragging and it's still not offering to make a connection, try dragging from the document outline (also shown in the screen shot).

You could then edit that progress indicator from other view controllers, which are descendants of that window's view hierarchy, using code like this:

    if let windowController = self.view.window?.windowController() as? CustomWindowController {
        windowController.progressIndicator.doubleValue = 0.4
    }

or, in Objective-C, something like this:

    CustomWindowController *myWindowControllerSubclass = self.view.window.windowController;
    windowController.progressIndicator.doubleValue = 0.4;

Hope that helps.

Volin answered 18/12, 2014 at 20:35 Comment(2)
AHA!!!!!! PERFECT! but how do I access this outlet from master or detail controllers? Sorry about the questions but I am very new to cocoa apps. This is my first cocoa app that involves this level of complexity and the first also with storyboard.Frazer
If a control generates an action, the action is typically put into the view (or window) controller that owns it. Your progress bar is owned by the toolbar which is owned by the window, so its actions would normally go in that Window controller. If they then have to call elsewhere, you'll need to wire that up using direct calls, delegation, notifications or some other technique that communicates between controllers. A direct call from Window might be "self.contentViewController.splitViewController.splitViewItems[0].myFuncOrProperty" but you'd add typecasting.Volin

© 2022 - 2024 — McMap. All rights reserved.