Loading a custom view in a storyboard from a separated nib file - ios

I've customized an UIView in its own .xib file in order to being able to reuse it in several scenes of my storyboard. It is only a view with no view controller behind. I'm trying to load it in the storyboard and to see it in Interface Builder in the scenes I want to place it, with no success. I've been looking for a way to do that and I didn't found a post with an answer that worked for me.
I'm using Xcode 6.3.2 and my app is targeting iOS 7 and above. Could somebody help me?
Thanks in advance

What I do is make a subclass of UIView with its xib (CustomView.h, CustomView.m and CustomView.xib). And add a UIView to your controller on storyboard. Then assign its class to your custom view class.
This works without loading custom xib via code. But you cannot see the subviews of your custom view on storyboard.

Related

I have One storyboard with different/multiple UIViews and I want a ViewController for each UIView. Swift

I have one storyboard with different views, I do not want all the codes in ViewController.swift I would rather have a Swift for each UIView.
Is it possible? and how do I do?
Use UIContainerView instead of just UIView. When you drag a UIContainerView into your storyboard it'll automatically create a View. Just make a new Cocoa Touch Class file in your project. Make sure the file is put under UIViewController
Then click on the new View that was automatically generated, go to Properties, then select your new Swift file. This connects the View to the file.

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.

Cannot control-drag in xcode between cocoa touch uiview class and subview in storyboard

I'm new to xcode and iOS.
I'm following apple tutorial, but I can't figure out a thing.
In the tutorial, when there is the need of a connection between the storyboard and the ViewController.swift, it's done a connection using the control-drag from the storyboard to the viewController.swift, and it works like a charm.
If I add a subview to storyboard, apple does the connection and the declaration by code in a custom cocoa touch class that implements UiView Protocol, not using the control-drag.
Now, given the fact that is clear to me the code writted by apple, I'm just wondering why the control-drag doesn't work.
I've tried to do the same thing, adding a button via ui and then tried to control-drag to cocoa touch class, and I cannot do this.
But if I do the same thing, but trying to connect it to the ViewController.swift it works perfectly.
Now, I'm just wondering why this happens.
Can't I use control-drag with a custom UiView?
Thanks for answering.
You can only control-drag from outlet to view on xib, after creating outlet in code you will see a dot at the left side, you can drag from there to your view and outlet will work
In the storyboard, you have to set the class name to the view controller.
If you want to create a custom view that will have subview of it's own, you can do so using a Xib file (pretty much like a storyboard, but for only one view instead of for a whole app workflow) and then you'll be able to make all the connection you wish. Storyboard is not meant to design your customView. Connection are mainly used to provide a easy/cost-effective reach from the control grasp to it's main view's subviews (IBOutlets) or to '0 line' a target action pattern. One exception is with the custom tableView / collectionView cell though..
For connectioning between storyboard & class
you must in the
storyboard -> customClass -> chose your class

Custom UIView in Storyboard

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

Using NIBs in UIStoryboard

For an ipad application in ioS5.0 using arc and storyboard, i wanted to create another custom view and instantiate several instances of that view within a scrollview so as to show a scrollable train of these custom views. To do this, I created the custom view in XIB file, however, how should I instantiate this view in the ViewController being refrenced within the storyboard. The custom view also has a gesture recogniser within it.
thank you in advance for your help on this.
I don't think there is a way to instantiate a nib backed view from a storyboard. You can do most of the layout in your storyboard, but then you will have to programmatically build your nib backed views and add them to the scroll view.
As a side note, it sounds like you are building something that could be solved with a UITableView and a prototype cell. Here is a pretty good tutorial.

Resources