calling xib inside the Storyboard - ios

Can I call or integrate my existing xib files with their corresponding .h and .m to the Storyboard? I have around 6 xib files with their layout inside and i need to call them inside my new storyboard.

You can't call your xib files in a storyboard, but you can copy and paste any views from your xib to the storyboard, and change the class of any of your storyboard view controllers to the class of the controllers that owned your xibs.

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.

Load xib from Cocoapod in Storyboard

I have a storyboard where I want to embed a viewcontroller in a containerview. The ViewController and its xib are in a cocoapod I made. Is it possible to embed a viewcontroller and its xib from a cocoapod in a storyboard containerview?
I know that if you delete the viewcontroller's view (in the storyboard) then the storyboard will try to load the xib with the same name as the viewcontroller. However, this isn't working. The app crashes when the viewcontroller's outlets aren't linked up. I can only assume it's because the xib is in my cocoapod.
I've checked that the names match and all the appropriate custom types are set in IB (like changing File's Owner from UIViewController to MyCustomViewController). I added the xib to "resource_bundles" in the podspec too.

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!

Copy and paste storyboard controller into xib file. Supported or not?

I want to refactor out a storyboard controller to a separate xib, for better reusablity.
The controller extends UITableViewController.
I can select the controller in the storyboard, hit Cmd-C, create an empty xib file using the New File wizard, and finally paste the controller into the xib.
It seems to work ok. The UI elements are there, the outlets are there, etc. It forgets the orientation and the size (portrait, Retina 4-inch), but I can easily set that in the properties pane to the right in xcode.
However when trying to use this xib I get a crash with error message:
loaded the "MyController" nib but the view outlet was not set.
When I try to connect Files Owner to the table view (which is the top view in a controller extended from UITableViewController), xcode won't let me connect them.
My question: Is it unsupported to copy-paste a controller from a storyboard to a xib?
You are most likely jamming a tableViewController into a nib and there is no outlet to hook the view to. When you do Ctl-C in the storyboard make sure you copy the UITable not the UITableViewController. If not you can try to manually create the outlet in the controller but you might have to jump through hoops to make it work.

How can I create view controller that is defined in XIB?

Sorry for perhaps noob question, guys. I'm new to iOS development.
How can I create view controller that is defined in XIB ?
When you create new class inherited from UIViewController in Xcode via "New File" and mark with XIB checkbox it creates xib file that doesn't have viewController element. But rather it has placeholder which points to UIViewController which is defined in the code.
But I see that you can create controller itself in Interface Builder and specify customClass for it.
Unfortunately it doesn't work for me. I get loaded the "EmbeddedViewController" nib but the view outlet was not set error in run-time. But I believed view property must be set by resource loader automatically since everything is defined via XIB. It works that way for TableView and its cells for example.
Could you please provide example how to do it ?
I created sample project: https://github.com/cppexpert/SampleWithNib
The controller in question is EmbeddedViewController
Create one Xib per UIViewController
Each Xib have File's Owner object for you to set UIViewController class there. Click File's Owner and choose EmbeddedViewController on it's class
Then drag main UIView not UIViewController class to there, then hook up this view with file's owner as view. UIViewController just use to drag to StoryBoard base project.
https://github.com/lequysang/gitfiles02/blob/master/SampleWithNib-master.zip
Turned out these controls exist in IB for Storyboard projects.
When you create a view controller with xib via "new file", Xcode generates an UIView and connects it with view outlet automatically. Seems like you changed something after Xcode generated the xib file. In that case you need to connect a view to the view outlet manually.
If you create a new view in xib.set the file owner to your custom class.
Or if you simply want to create a view.
Then
NSArray* test1 = [[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil];
self.myViewFromNib = [test1 objectAtIndex:0];
Where myViewFromNib is your view object and "View" is your nib name.

Resources