Custom UIView in Storyboard - ios

I can't work out if I am doing this with complete retardation or not.
I would like to add a custom view to my view controller in my storyboard.
I drag a view object on to the VC and set the class to my custom view class.
I would like to then have a xib file associated with this class so that I can design a user interface for this class separately from the storyboard.
The solutions which seem to be hacks referenced on stack overflow (adding the xib as a view in initwithCoder) have not worked for me, and the prospect of programatically aligning a load of images and labels when dragging and dropping would be so much easier really frustrates me.
Is there some easier method I'm missing, what is everybody else doing?
Thanks,
Alan

With storyboard there is no xib file
hack your items to .h
press ctrl and drag a link to viewcontroller -at the bottom of ur viewcontroller in storyboard, or link it to your .h file and name it

Related

UIViewController defined in a xib file doesn't load in the storyboard

I'm trying to load a subclass of a UIViewController with its views defined in a xib file into a storyboard. Let's call it a NibViewController.
The point of this approach is to reuse the same ViewController in multiple screens of the app.
I know it's possible to do it manually in the code, but I'm looking for a solution in the storyboard. I've tried suggestions from other topics like this one, but nothing worked. The ViewController is correctly displayed in the simulator but not in the storyboard. Here is the code: https://github.com/srstanic/NibViewControllerInStoryboard
and here is the screenshot:
Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?
Am I mistaken to expect the contents of the NibViewController to appear in the storyboard?
Yes, you are mistaken. Your app is working perfectly so you should stop worrying and just proceed.
By deleting the view from the view controller in the storyboard, you have specifically instructed the storyboard: "Do not make a view for this view controller. At runtime, the view should come from the xib file, not from you."
And that is exactly what does happen at runtime. So just design your interface in the xib file and all will be well.

How to create a separate view in Storyboard to be included programmatically in UITableView?

I have a UIViewController with a UITableView that is fed with data from the local database. When the user first launches the app (after installing) the table view is empty and I display a UIView in the middle of the table view that contains a UIImage, a UILabel and a UIButton (a call to action).
The first version of this view I built programmatically, which was no good to me because every time I tweaked something I had to build the app again. Then I switched to the storyboard but had to drag a UIView to the middle of my tableView. It is working now but I don't like the way it is, I can't edit my table view cells without having to move the UIView out of the table view.
I'd like to have a way to build this view entirely separated from my tableView (or even from my view controller in question) and then reference it in the viewDidLoad call of my view controller.
Unfortunately Xcode does not allow us to drag views directly to the storyboard so I'm pretty lost here.
Please tell me if I haven't been clear enough. I appreciate any help you could give me.
UPDATE: It'd be particularly awesome to me if I could also create a custom Swift class for this view of mine and reference it in the storyboard. And then in my viewDidLoad I could simply instantiate my custom view.
Thanks in advance.
Create a XIB file in which you can drag a view (without a view controller).
In your code you can load the XIB using NSBundle.mainBundle().loadNibNamed("MyXibName", owner:self, options:nil).
In the XIB file you can give the UIView a custom class (like you can do with view controllers in storyboard).
You then of course have to retrieve the view from the array returned by loadNibNamed and cast it to your custom class.

IOS Custom UIView doesn't show

I'm sorry but i'm still a bit dumb in UIViews and creating a custom one.
I have a custom view and a xib file that are connected through the XIB IB.
I want to add this view to the storyboard.
What you see in this picture is an empty view which i've connected to the custom view class
When launching the app - i see the exact same view without my custom view being loaded.. what am i missing ?
It is not enough to just link your xib view to the storyboard through the view class name. You should copy the view from your xib (open your xib, select the root view and copy) to your storyboard view controller (select the parent view and paste). You can even open (double tap the xib file in the navigator) your xib in a new window and do a drag drop to the story board. Note that all the property wirings and constrains that you had defined in the xib will be retained when you copy paste your view from xib file.
The answer is actually pretty simple.
I had a recursive init process - meaning that although i did over initWithCoder method, in the xib file, i've set the view class to be my class.
What i should have done is to make my xib file's owner to be my class instead.
And then all the loadFromNibName worked perfectly in my initWithCoder!

how to place a UIViewController in another UIViewController through nib in xcode

I have developed a DetailViewController, derived from UIViewController. Now I want to use this DetailViewController inside another InfoViewController's layout. I found one way to do this by using addSubView method. But what I am looking for is to plot it through nib file, like other UIViews. I want to plot DetailViewContoller inside InfoViewController's layout through nib file. Is this possible?
Any response will be thankful.
Yes it is possible..
First remove the referencing outlet for view in DetailViewController.xib.
And in InfoViewController.xib drag-drop Object and edit the custom class to DetailViewController.
Now drag-drop view in InfoViewController.xib and add what ever subviews you want to it. Now add new view referencing outlet to DetailViewController which is under objects.
Hope this will help you.

How to connect UIView outlets to a custom subview

I'm still new to xcode / iOS and have the following problem:
in order to display some mobile debug information, I have a UIview added/connected as outlet-property to one of my Viewcontroller. This view is a custom subclass of UIview. Now I addeddd some UIlabels as sub views to this view and want to drag the outlet connections from these labels to my customUIview.h file in order to have these labels accessible as properties of my custom UIview class (no need to access them directly from the view Controller).
Problem is that the interface builder (I'm using Storyboards/ xcode4.3) does not make the trick. I can connect the outlets to the ViewControllerClass.h but not to my sub view's .h file.
Can anyone point out where the problem is?
Just solved the problem. After typing the property outlet declarations manually in the customview.h file I could ctrl-drag-connect them from there to their corresponding UIlabel objects in the interfacebuilder. Works only in this direction!
Thanks anyway
Update your custom view class from "UIView" to your "Custom View" class in interface builder.
And now you can make connections just by ctrl+drag your buttons or textfield to your appropriate custom class.
Problem solved! Due to an interesting SO post from three years ago (about connecting to subviews of UIView), I discovered that one merely drags (not Ctrl_drag!) from the action or outlet circle (in the .h file) to the control and that's it. Works perfectly even when the controls are in a different view from the subclassed UIView. Works equally well with outlets as with actions though you always drag away from the circle.

Resources