Creating an outlet for a NSProgressIndicator insid

2019-06-05 00:22发布

问题:

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

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?

回答1:

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

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.