Use NSToolBar Outlet xcode 6 and Storyboard?
Asked Answered
R

4

0

I am trying to add an outlet into my viewcontroller for a toolbar item in my window controller. I have tried playing around with first responder and bindings but have not been able to find any solutions.

A similar question that was answered provided some insight but no one has mentioned anything about IBOutlets other than still asking how to add them in the comments. The answer has been accepted so i am assuming no one will add to it.

How to use NSToolBar in Xcode 6 and Storyboard?

Incase my question is unclear at all, i would like to be able to add this to my storyboard program

@IBOutlet weak var Mytoolbar: NSToolbarItem!

func enabletoolbar()
{
    Mytoolbar.action = "FunctionIn.ViewController.swift"
    Mytoolbar.enabled = true
}
Richerson answered 9/12, 2014 at 4:27 Comment(0)
R
0

i ended up doing this in my view controller which seems to work

override func viewDidLayout() {
    var x = self.view.window?.toolbar?.items[1].label
    println(x)
    if(self.view.window?.toolbar?.items[0].label! != "Check")
    {
        toobarediting()
    }
    println("didlay")
}

func toobarediting() {
    self.view.window?.toolbar?.insertItemWithItemIdentifier("Check", atIndex: 0)
}

func toolbarcheck(functiontoset: Selector) {
    var y = self.view.window?.toolbar?.items[0] as NSToolbarItem
    y.action = functiontoset
    if(functiontoset != nil)
    {
        y.enabled = true
    }
}

It seems to allow me to make the tool bar button clickable/unclickable when ever i require it to change it just seems so much more bulky and error prone than

myitem.enable = fale 
myitem.action = nil

is this really the best way for a storyboard based application in osx?

Richerson answered 9/12, 2014 at 8:33 Comment(0)
B
1

I found a decent workaround by adding IBOutlets to my custom NSWindow class and using the storyboard to connect my views to the IBOutlets. Then, I accessed these views from my NSViewController class by getting them from the custom NSWindow.

Bellini answered 13/7, 2015 at 7:2 Comment(1)
This was very helpful for me, with a minor alteration. Instead of subclassing NSWindow, I subclassed NSWindowController, conformed to NSTextFieldDelegate and implemented controlTextDidEndEditing() and controlTextDidChange(). In these methods I use NSWindowController's contentViewController property to call my custom search method in the NSViewController class, passing the search string as an argument.Raggletaggle
K
0

Basically you need to set the action and other properties to the toolbaritem but not in the toolbar. So try the same.

Karisakarissa answered 9/12, 2014 at 7:56 Comment(0)
R
0

i ended up doing this in my view controller which seems to work

override func viewDidLayout() {
    var x = self.view.window?.toolbar?.items[1].label
    println(x)
    if(self.view.window?.toolbar?.items[0].label! != "Check")
    {
        toobarediting()
    }
    println("didlay")
}

func toobarediting() {
    self.view.window?.toolbar?.insertItemWithItemIdentifier("Check", atIndex: 0)
}

func toolbarcheck(functiontoset: Selector) {
    var y = self.view.window?.toolbar?.items[0] as NSToolbarItem
    y.action = functiontoset
    if(functiontoset != nil)
    {
        y.enabled = true
    }
}

It seems to allow me to make the tool bar button clickable/unclickable when ever i require it to change it just seems so much more bulky and error prone than

myitem.enable = fale 
myitem.action = nil

is this really the best way for a storyboard based application in osx?

Richerson answered 9/12, 2014 at 8:33 Comment(0)
B
0

While connectiong IBActions works by using either the First Responder or by adding an "Object" to the scene, then changing its class to the window's view controller class, this doesn't help with IBOutlets and delegates that you'd like to point to the view controller.

Here's a work-around for that:

Add the Toolbar to the View Controller, not to its Window. That way, you can make all the IBOutlet connections in the View Controller Scene easily. I've done that for years and found no issues with it, even when using Tabs.

You'll have to assign the window's toolbar in code, then. E.g. like this:

@interface ViewController ()
    @property (weak) IBOutlet NSToolbar *toolbar; // connect this in your storyboard to the Toolbar that you moved to the View Controller Scene
@end

- (void)viewWillAppear {
    [super viewWillAppear];
    self.view.window.toolbar = self.toolbar;
}
Bobbybobbye answered 6/7, 2021 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.