I'm coming from the world of C# and Winforms where I can build a custom UserControl and drag it onto a form as if it were a common control. Is there a way to do that in Xcode and Interface Builder?
I've only seen how to set the view at runtime but I'd like to see it on my window at design time. For example, I'd expect my custom view to be listed in the available controls.
Thanks!
Simon
Interface Builder doesn't add (or allow the addition of, now that IB Plugins are derelict) Custom Objects to the Object Library. You can only drag in existing/preset objects, but you can have IB instantiate them as instances of a subclass as long as they are instantiated the same way.
E.g. Drag in a custom view and set the class to MyCustomViewClass, or drag in a scroll view and set the class to MyCustomScrollView, or drag in an object and set the class to CustomObject, et cetera.
And that is the preferred way of using custom controls in Interface Builder.
Basically just ripping off rdelmar's comment... but I thought a picture would be helpful
Drag a normal view into IB, then select it and do this.
Every Window you create in IB has a view on it as its first subcomponent. You dont need to create a separate custom view for it. If you really are subclassing it just change the class name in the inspector.
In addition to what has been mentioned in this thread about adding your custom view's name to the view controller's XIB file, you will need to override initWithCoder in your custom view.
Related
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.
Sorry if this has an obvious answer, I can't seem to find anything describing this situation. How can I set up a nib file which directly implements a custom UIView? In other words, the top-level view in the nib file becomes an instance of my custom UIView class? The closest I've been able to find so far is to create a custom UIView which loads the nib and sets it's top-level view to a UIView* property (and all it's subviews to the appropriate outlets by loading with owner:self). This works, but as I understand the top-level View does nothing except act as a container for all the other views - which is what my custom UIView is supposed to do. Any suggestions?
Edit: I got this in the end: the issue is my custom view class was set as the File's Owner class, when it should have been set as the top-level View's class. I didn't know it was possible to set outlets on elements in the IB as well as the Owner. Setting the Owner to the class of the ViewController that will hold it and the top-level view to my custom view has it all working.
If you create a project using the single view template, you will get an empty view. If you add a class that is a subclass of UIView, you can change the class of that view in IB to your class (in the identity inspector).
If I have understood your question correctly, then this should help -
In the IB, on the right-pane. select the "custom class" dropdown & fill in your custom UIView class. The image shows UITableViewCell instead of that put your custom UIView class name.
Correct me if I have not understood your question correctly.
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).
I need to create a custom window, so I have created a subclass of NSWindow. I overode the constructor and a bunch of other functions.
I need to replace the current window with the window subclass I created. I know it has something to do with Interface Builder but have no idea what to do. How would I do this?
I tried removing the NSWindow from the Inspector, and instead adding my custom window, however this only results in no window showing up during runtime.
I also edited the app's delegate to change NSWindow to my custom window and also changed the delegate's "main" outlet to my custom window.
I am on Mac OSX Snowleopard using Xcode 3.2.6
First, you need to make sure Interface Builder knows about your custom class. To do this, open your xib file and go to "File->Read Class Files…", and choose your header file.
Then, select the window you want to change and open the inspector. In the last tab of the inspector, under "Class Identity", there is a text field labeled "Class" simply type your class's name here.
After you do this, your custom class will be used as the class for that window. It will look like a normal window in IB, but will let you use custom outlets and actions, and will use your class when you actually run your project.
go to the third tab of the utilities section when your window is selected
enter your custom class name under 'Custom Class'
I'm using Objective-C and Cocoa, whilst developing for Mac OS X - so not the iPhone/Cocoa Touch. (That said, I'd be interested if it was the same procedure for the iPhone)
I'm working on a preferences window for a simple app. I have a NSWindow with a toolbar - there are 5 different items on the toolbar, all of which need to bring up a different set of options.
So I set the NSToolbar and its items in Interface Builder, and then placed a custom view underneath the menu - taking up the rest of the window. My plan is to work out the interface too each of the NSToolbarItems options, and then draw the corresponding view on to the custom view when the specified NSToolbarItem is clicked.
I'm guessing that I simply create a NSView sub-class for each view, an empty xib in Interface Builder - set the xib to my custom NSView, code it as usual... But here's a few problems;
1 - Just how can I get the xib file to appear on the custom-view then? I have looked around and most articles don't seem to have this situation, or a situation I can relate too.
2 - When the window comes up, I want the default view to appear on the custom view. Once again, I'm guessing I just write that in the initialisation code for the NSWindow - its no big deal. It just goes back to question 1 though - how do I draw my NSView to the custom-view specified in Interface Builder?
I'd be really grateful for any help!
Cheers in advance.
So I set the NSToolbar and its items in Interface Builder, and then placed a custom view underneath the menu - taking up the rest of the window.
You can't have a menu inside of a window. You can have a pop-up button, which has a menu, but not a menu directly. Did you mean “toolbar” here?
You don't need to create a custom view for this. Make a tab view and set it to be tabless. Give it as many tab view items as you have toolbar items. In your controller, write an action method for each of the toolbar items, and in each action method, switch the active tab of the tab view.
You can activate different tabs in IB to populate them with views in IB. The active tab is saved in the nib, so make sure you set it back to the first tab before saving, so that the first tab is the one that's initially active when your app runs.
Just how can I get the xib file to appear on the custom-view then?
That question doesn't make sense.
Once again, I'm guessing I just write that in the initialisation code for the NSWindow - its no big deal.
You would only be able to do that if you have your own initialization code for the window, which you would only have if you have subclassed NSWindow. There are very few reasons to do that; unless you're making the window itself look different (not making an Aqua or HUD window), you should move that initialization code elsewhere, probably to the aforementioned controller (which should be the File's Owner of the nib).
It just goes back to question 1 though - how do I draw my NSView to the custom-view specified in Interface Builder?
A custom view in Interface Builder is a plain NSView (unless you explicitly change it to a subclass of NSView you create). However, you do not need one for anything you have described in your question.