IBAction inside of switch statement - Xcode - xcode

I have over 30 buttons and 30 images inside of a switch statement, 4 buttons showing at the same time. I want to perform an IBAction depending on which button is pressed. It's not allowing me to drag & drop it into the case inside my switch statement. The buttons change depending on what image is showing. For every image, there are 3 wrong buttons, and 1 correct button. How can I check for which button is pressed, and add a resulting action?

You are getting it totally wrong. You should use switch statement to control look and feel of UI. Like controlling the 4 buttons to be shown by simply using hide property.
Each button should have an IBAction defined at class level. So, drag and drop connection from XIB/Storyboard to class and not inside switch statement. Depending on which button is currently shown, it action would be triggered.
EDIT: Post OP clarification:
Since you are trying to set different image on same button based on some condition and want to handle the IBAction based on the kind of image you set on it, you would need to set the tag on the button in your switch statement where you are setting images. tag takes an integer value so you can create enum for readability. Then when button is tapped and your IBAction is triggered, put another switch statement to check the value of tag and then take action accordingly.

Related

Show NSSearchFields clear button at all times

I want to use the clear ✖️ button of an NSSearchField to close a search pane. Therefore I have manually set the action and target of the clear button cell. Now the button only appears when the search field has text in it and I can't find a way to override this behavior.
Is it possible to show the clear button at all times, wether there is text or not?

Drop-down menu in NSToolbar like Mail.app

I'd like a toolbar button with an attached dropdown menu, like the "Flag" button in the toolbar in Mail.app:
I'd hoped that making a normal NSMenuItem and adding a menu as the menuFormRepresentation would do the trick, but that menu only appears when the button goes into overflow mode.
I had also hoped that adding an NSPopupButton as a custom view would work, but that makes the whole view a menu, whereas I want the left part of the component to behave like a normal button, and the right dropdown part bring up the menu.
Is there some trick to making the NSToolbarItem show a component like this, or is this two custom views stuck together?
There's nothing magical about NSToolbar here. That's just one of the ways you can set up NSSegmentedControl, regardless of whether it appears as a toolbar item's custom view or on its own.
You can't set this up in Interface Builder (storyboard), but NSSegmentedControl has APIs for assigning menus to segments:
segmentControl.setMenu(myMenu, forSegment: 1)
segmentControl.setShowsMenuIndicator(true, forSegment: 1) // for the little arrow
You probably want to set the tracking mode to momentary, since your segment control is acting as a set of visually-connected buttons, not a choose-one-of-N selector.
When the user clicks either segment, your action method will need to use the selectedSegment to decide whether to perform the action associated with the "button" side or ignore the click (letting the menu show for the other side).

XCode: Graphical button with invisible title

I have a bunch of buttons. They appears as an graphical image. If a user clicked on a button I can determine with
sender.titleLabel!.text!
which button the user pressed. But the title of the button appears in the view. I want only to show the image and give the button a invisible title. But I think that is not possible.
Me second solution is to create for each button an outlet. But I think with 30 buttons that is a very bad solution.
Option 1:
For the button text color property set opacity to 0. The text is there, but fully transparent.
Option 2:
You may use the tag value to identify a button so you do not have to rely on the button title. You can set the tag value in interface builder (Xcode) or in code. (The tag is an integer.)
I usually prefer option 2 as it is resilient to text changes over time (think of typos, translations for other languages etc.).

What is the difference between "Multiple" and "Single" for View Controller Presentation?

I've been working on an OS X app in Xcode. An option that completely perplexes me is "Presentation", with the two options "Single" and "Multiple" what does this attribute do?
So, this was actually "obvious" once I used it.
Basically, this feature causes a window to be displayed once, or multiple times if the corresponding segue in a storyboard has been triggered multiple times.
To see this in action, add a create a storyboard with a view controller in it. The place a button on the view, and an additional window controller. Create a segue between the button and the window controller to "show" the window controller.
Click on the window controller and toggle between the two Presentation options. When you run it, you will find that one case will create multiple instances of the window, while the other will create a single instance of the window.
Like I said, obvious, but had to actually use it to figure it out.

How do I check if a NSView is being displayed at the moment?

I got an application which has a NSToolbar in its main window. Depending on which icon is clicked a NSView is displayed in this window. My problem is, that one of these views shows data in a NSTableView that I want to be reloaded each time the view is visible. Since -init is only called once, I don't know how to do that.
(example: When the application starts it shows the Documents section [on of the sub views of the window]. Now when I click on Employees [which displays another sub view instead of the first one] and then on Documents again, I want the data in Documents' NSTableView to reload.)
How do I do that?
Thanks in advance.
I got an application which has a NSToolbar in its main window. Depending on which icon is clicked a NSView is displayed in this window.
Use a tab view. You can hide the tabs, then implement your action methods for the toolbar items to act as the tabs, changing the selected tab view item to whichever one corresponds to the pressed toolbar item.
Now when I click on Employees [which displays another sub view instead of the first one] and then on Documents again, I want the data in Documents' NSTableView to reload.
Why? Why not reload it only when the data changes?
You don't have to hold NSTableView's hand; if it needs the data from you again, it'll ask you for it again.
And if you're concerned about reloading the data while the view is not visible, that's premature optimization. Don't worry about it until you prove via profiling that it is a real performance problem.

Resources