Adding a custom view to a window in Cocoa - objective-c

I am creating a sample application for Mac OSX. I created a window which contains a login button and I added a new custom view. Now I need to load the custom view to the window when the user clicks the login button. Please anyone help me...

Will this help?:
[[window contentView] addSubview:customView];

[window setContentView:customView] will also do the job.
You also need to add IBOutlet variables for your window and custom view to the header file for your controller, and drag that onto the Interface Builder document window. Then control drag from the controller to the window and view, and select the variables.
(and you need to change the controller to be an instance of your custom controller class, which you do by selecting it in Interface Builder and going to the Inspector)

Related

Add a view defined in a xib file in a Window in the soryboard using Xcode & Swift

I have a window in the storyboard and a box in defined a xib file. I would like to see the box appearing on the window as soon as the app launches.
The box is also defined by a custom cocoa class that I created and I associated to the xib file. The class is called Box.
Why does the code in the picture doesn't show the box in the window? The code is in the NSViewController of the window.
Thank you
Try to replace window.addSubview(box) with self.view.addSubview(box).
You need to display your view as a subview of your root view controller.

Adding a new window in Cocoa

I'm a beginner with Cocoa programming. All I wnated to know is how do I create a new window in Cocoa. I have created a NSViewController and window. As shown in tutorials I was able to add buttons to the MainMenu window and add actions to the buttons. Now I added a new NSViewController. I then went to plist file and made to load it first. My questions are as follows.
When adding a new NSViewController, h m and xib files were created but the XIB file just has custom window and no other control elements like minimize max buttons etc.
Secondly I added few elements and ran the file. The application ran but nothing was displayed on screen. Am I missing something.
You mostly only create a new NSWindowController if you add a new Nib-File.
The advantage of separating your windows or separate views in Nib-Files can be:
Making your code more organized
Easy instantiation of multiple windows of the same type (like browser windows)
If you don't need this, you can simply add a new window to your MainMenu.xib.
Then you can either let the window be visible at launch, or make an outlet to it and make it visible whenever you desire.
Otherwise you can go to your files -> Add new file -> Subclass of NSWindowController
There will be a checkbox to automatically create the xib-file for you, make sure to check.
Now just make sure to initialise with initWithWindowNibName:, and call showWindow: on it.
MyWindowControllerSubclass *wcs = [[MyWindowControllerSubclass alloc] initWithWindowNibName:#"TheNameOfMyNib"];
[wcs showWindow:self];
I was using XCode 4.2. Now I updated to 4.6.3. Now while creating NSWindowController xib file creating option is there. SO its done directly.
Otherwise you need to add a window controller and a new Window XIB. Later you can add an Object to properties tab. Then change the class to the Window Controller and link the Window to the Object.
If you want to add the view controller to a window then, it is fairly simple. NSViewController has a property named view. You would add this view to the window.contentView.
You would typically create a new xib file with a view and setting the file owner to your view controller. Then in the -(id)initWithNibName: method you could call the super with the nib name of your view to instantiate the controller with its view.
If you want to show a new window. Then create a new NSWindowController object and instantiate it, with the nib of your window and calling window on the controller should bring the window to front. If your window has already been initialised then, you could call methods like orderFront:, orderBack: on the window of the controller.

How to connect dataSource outlet of a Page View Controller using Storyboard in Interface Builder

According to Apple's documentation here, we should be able to add a Page View Controller into the storyboard and then optionally set the data source by connecting the outlets.
Creating a Page View Controller Interface Using a Storyboard
The Page-Based Application Xcode template creates a new project with a page view controller as the initial scene.
To add a page view controller to an existing storyboard, do the following:
Drag a page view controller out of the library. Add a page view controller scene to your storyboard.
In the Attributes inspector, set up the appropriate options.
Optionally, set a delegate, a data source, or both by connecting the corresponding outlets.
Display it as the first view controller by selecting the option Is Initial View Controller in the Attributes inspector (or present the view controller in your user interface in another way.)
I then defined a UIPageViewController subclass like so
#interface DetailsPageViewController : UIPageViewController <UIPageViewControllerDataSource>
but then when I tried to connect the data source outlet, it does not highlight the controller or allow to connect it. I have also tried implementing UIPageViewControllerDataSource on other controllers but I have the same problem of not being able to connect the outlet.
Can anyone help?
I failed to find a way to do it in IB. Have to use the following instead:
self.delegate=self;
self.dataSource=self;
Note that the Apple documentation states that UIPageViewController is not normally subclassed. Your UIPageViewControllerDataSource does not need to be a subclass of a View Controller. You can make it a subclass of NSObject.
Normally only things that appear on the storyboard, namely UI elements, are listed in the document outline that appears to the left of the storyboard (provided it has not been hidden). If your delegate/datasource is not already there, you can put it there, by dragging an 'Object' (yellow cube) into the document outline, in the appropriate scene.
Then click on the Object that you just added, and use the Identity Inspector pane to alter its concrete class to your data source class. It's then available to be used as the target of a connection in the normal way by dragging a line from the Connections inspector onto it.

In Interface Builder, how can I add UIViews to a subview that is a part of a custom view I've created?

I am working to create a custom view for an iPhone app I'm creating. This custom view is a Popover dialog which is made up of a UIView which contains two images, a button to close the dialog, a label, and a UIScrollView. This view is named MDPopoverCard. I have these files as a part of my view:
MDPopoverCard.xib - The view as drawn up in Interface Builder.
MDPopoverCard.h - Defines a few IBActions and some other properties
MDPopoverCard.m - Implements some functions defined in the header
This is what it looks like in Interface Builder: http://cl.ly/2B0f2x3s1w1i0K2G0Q1r (sorry, I can't post an image yet as I'm new to stackoverflow)
There are a few properties defined in my .m and .h files that control whether the green button is displayed and what the text of the title label is.
I need to display a number of these dialogs in my app and I'd like to reuse this interface I've designed. I want to be able to add buttons and other form elements into the UIScrollView via Interface Builder. However, I have a problem:
Imagine that I have another view I'm drawing up in Interface Builder. I add a UIView to it and set its class to MDPopoverCard. I then drag a couple UIButton objects into my MDPopoverCard view. Here's an example of what it looks like in Interface Builder:
http://cl.ly/1X090h1t1q3f0i3E0917
This screenshot shows another view (the root view) that I've added my MDPopoverCard to. I've then added two buttons as subviews of MDPopoverCard.
These buttons do get properly nested in Interface Builder. However, when I run my app these buttons are added before any of the items that make up my MDPopoverCard view in the xib file. This means that the buttons are being added behind my popover dialog. That's the first problem.
The second problem is that I want these buttons and form elements to actually be added into the UIScrollView that's contained within the MDPopoverCard view, and not just right into the UIView's subviews array. Is there a way to specify this in Interface Builder? I'd really much rather draw buttons into my UIView and connect them to IBActions via Interface Builder than hand write every instance of these dialogs that I may need to display (several).
Any advice? Is there anything I can do to clarify the question?
Thanks for your help!
Formerly Xcode supported user-defined IB plugins for custom UI elements which you could just drag and drop into the XIBs the same way you do with built-in widgets. As of Xcode 4 this nice feature has been removed. (Thanks a lot, Apple.)
Currently I can only think of a hacky way to achieve what you described. What I would do is the following:
create an IBOutletCollection on your MDPopoverCard, e.g. embeddedControls
link it with every UI element (here: the buttons) you want to go inside the scroll view
implement the awakeFromNib in MDPopoverCard and explicitly reset the superview of all the views in embeddedControls to the scroll view in there
Hope this helps (although I haven't tried).

Putting an NSPopUpButton in an NSToolbar

Having wrestled with NSPopUpButton in a previous question here I am now trying to place a NSPopUpButton inside an NSToolbar. Essentially I want to create something similar to what XCode has by default on the left hand side of it's toolbar. E.g. a pop up button with an action button next to it.
I have seen a method that show's a programmatic way of creating an NSPopUpButton and then adding it to an NSToolbar, but then I can't work out how to do all the Binding stuff that was so handy last time.
Interface Builder hasn't been very helpful, so any help gratefully received.
P.S. Could I solve this by creating a custom view (containing an NSPopUpButton with the usual bindings) and then adding the custom view to the toolbar?
It's actually pretty easy to do what you want here. In Interface Builder, switch to the tree view (the second button on the View Mode segmented control). Expand the window and the toolbar. Then, from the library, drag a popup button onto the toolbar. Interface Builder will embed a new popup button in a custom view for you automatically.
To actually put the button on the toolbar, double click on the toolbar in the window. This will bring up the customization sheet. You can drag the popup button to the desired location on the toolbar.
If you wanted to do this programmatically, you would create a custom view containing your popup button. Then, you'd need to assign it to a new outlet so you can refer to it programmatically. In the toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar method, you would create a new NSToolbarItem per usual, and call setView: to assign the custom view to the toolbar item.
Old post, but note you can also double-click the toolbar in the window and drag straight into the "Allowed Toolbar Items".
You will want to open up the toolbar view anyway so you can drag things in or out of the default toolbar items.