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

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.

Related

Can you define a Segue in the storyboard to self?

I have an app that represents a folder structure. I have a UITableViewController for the folders and files listing, and a UIViewController for the Documents.
I want to be able to recursively navigate through the folder structure, so I want to reuse the Folder UITableView multiple times while I let the user drill down a folder structure.
Is there a way to draw a segue from the UITableViewController to self so when I select a folder I present another instance of the view, but with the content of the subfolder?
I did this in previous versions of Xcode, but I cannot figure this out on Xcode 9.
You can use Storyboard Reference and Storyboard ID of ViewController in Interface Builder
Screenshot
Yes you can do it. Add a hidden button in view controller and drag & drop segue self view controller.
I've never created a segue link to the same view controller, but based on Halil's answer above, it sounds like it's possible.
Rather than messing with hidden buttons, though, why don't you give your scene a storyboard identifier, and then instantiate and push/present your view controller through code? You could put your logic in the table view's tableView(_:didSelectRowAt:) method.

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.

Xib to Storyboard issue in copying

I recently migrated from Xibs to Storyboard in Xcode 6. To do this, I created a new controller in storyboard, deleted the view in storyboard, selected and copied the whole view hierarchy in controller's xib and pasted it to storyboard.
I had to manually remap outlets in storyboard and it works.
The problem is I can not see any of the views in the storyboard, and so can not add any new subviews. All I see is the name of the controller and thats it (see image).
How do I get around with it ?

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!

Create View Programmatically in Objective-C Xcode 5

How should I go about creating a View for the storyboard programmatically? I want to access the labels from the first ViewController object made(automatically to call the IBAction methods of VC). I know that this first object of VC is the one linked to the view in the storyboard(?) and I need to change a label form another file, besides VC. I'm pretty sure the only way to do so would be to access the VC object that is linked to the view, or create one and not go with the default one that is created. If not, how would I go about accessing the labels of the view from another file?
You don't create storyboard objects programmatically. A storyboard is very basically an XML file Xcode uses to call different view controllers. The biggest advantage of using storyboards over NIBs is you can layout transitions or segues, and the advantage of NIBs or storyboards over initiating view controllers by code is obviously the visual interface. So your question doesn't really make sense.
If you want to reference a particular view controller's label from your storyboard you need to create a pointer to that view controller first, but changing it programmatically doesn't make sense because that's what storyboard is for.
That said you may just need to go look for your class name in your view controller's Identity Inspector in storyboard and then edit your label programmatically through an IBOutlet property.

Resources