How do I place objects (i.e. labels or buttons) in a tab view in an Xcode mac app, so that when I click on a different tab the associated objects appear.
For example, I want to be able to put some labels in a tab called "Data" and some buttons in a tab called "Choice". When I press "Data" I want the labels to appear, but the buttons to not, and the other way around as well.
Is there an easy way to do this? Do I need to make a different class for each tab?
Each of the NSTabView's tab consists of as NSView. Here either you can draw all the buttons, labels or any other controls. Even you can add your new view from same xib or load it from other xib.
In the following image you can see, I have used a tabview with two tabs and in the object view you can see two views having two different static text.
And the tab change/click event is fired automatically you dont need to worry about it, the view will be loaded on tab click/change.
Use the built in cocoa view NSTabView which you can find in interface builder:
Each tab has its own view where you can place your objects.
Related
If I drag a Tab View Controller to the storyboard of an OS X application, the tab view buttons seem to misbehave. Can you help me understand what's going on?
Here's an minimal example of a fresh project, where I simply replaced the default empty View Controller with a new Tab View Controller:
The highlighted Tab View is shown as No Shadow Tab View by default, which means that the Tab View's style is Tabless in the Attributes Inspector.
There are also two Tab View Items below the Tab View in the Scene's list.
If I build & run, the result looks like this:
The tab controls are visible, but the tab view has no bezel. It seems like the tab buttons that are displayed are actually the two extra Tab View Items, not the native buttons of the Tab View itself.
If I change the Tab View's style to Top Tabs instead of the default Tabless, I get a bezel, but duplicate tab buttons:
And if I change it to Tabless With Bezel, the bezel is below the tab buttons, instead of properly sitting midway under the buttons:
I can't figure this out. Why are there two sets of tab buttons to start with (with the "real" one hidden by default)? The two extra Tab View Items seem to be completely redundant, but they can't be deleted.
Is there a way to have a tab bar with a proper bezel when using Interface Builder and a Tab View Controller?
You need to set the style of the tabViewController to "Unspecified" and setup the included tabView.
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.
Is it possible to do navigation within the same window in a mac application ?(Like it is possible in ios apps).I want to show each view in the same window instead of opening different windows on a button click.
e.g When a user clicks a button then the next page should be loaded in the same window.(The next page will have nothing in common with the current page.)
You may use Tab View for easy switching between views on a same window.
UPDATE:
You may also customize your tab view , make it tabless (In the attributes inspector set style to tabless) and use your buttons to switch between views.
You may take help from the following link : http://devcry.heiho.net/2012/01/nstabview-tutorial.html
OR
You may add or remove subviews from your window on button clicks, using
[[yourWindow contentView] addSubview: yourSubview]; // Add subview to window
[yourSubview removeFromSuperview]; //Remove subview
UPDATE:
Steps to swap between views using a tabless tab view.
Drag a NSTabView to your xib.
Set the no. of tabs in attribute inspector to no. of views you want.
Design each view of the tab as per your requirement.
Now in the attribute inspector of tabview, set style to tabless.
Now drag the buttons you want to use for swapping between views. Suppose Button0 and Button1 are for 1st and 2nd view of your tab view.
Create a IBOutlet for your NSTabView in your .h file. Bind it to the referencing outlet of you tabview.
IBOutLet NSTabView* tabview;
Set a IBAction for both your buttons in your .h class file.
In the button action method for button1, use
- (IBAction)button1clicked:(id)sender
{
[tab selectTabViewItemAtIndex:0];
}
Similarly in button2 action method use:
[tab selectTabViewItemAtIndex:1];
In this way you can have any no. of views and you may select any view on button click using
[tab selectTabViewItemAtIndex:(index of the view you want to load)];
In general you want to google for view swapping.
There are tons of examples out there. Some from Apple and lots elsewhere.
Much of it is very similar to iOS.
You need to read the docs a bit too.
Understand NSView and how to load views from nibs, how to create view objects in code, how to add a subview and how to remove a view.
There are many approaches to having different views for different reasons. The right approach is a combination of style, experience and what your app actually needs to do.
Cocoa includes NSBox, NSTabView, and lots of others. Those two can be configured to not display any visual indication that they are containers.
You will also need to understand at least a little about NSWindow to understand its content view (the root container of other views generally)
I want to use the same sort of UI style of Xcode has right above the editor pane, as show below:
I've browsed through all the Views in Interface Builder and can't figure out what type of view this is. At this point I'm merely referring to the bar itself; not to the controls nested inside the bar, though I will be adding dropdowns in the same way as this.
What type of view should I be looking for?
TextMate uses one along the bottom of the editor too, which provides similar controls:
see:
$(DEVELOPER_ROOT)/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app
just open Accessibility Inspector.app and move the cursor over the views you're interested in for details (you may need to enable accessibility options in system preferences.app).
it's also helpful to press cmd+F7 to lock onto a view and then to inspect its children.
so... 3 primary options:
1) if you want drag & drop convenience, you can simulate it with a Gradient Button
2) or you can use the button with a custom image in the button to achieve something closer
3) or just create a new view subclass and render it using a CGGradient or NSGradient
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.