I need to create a new xib file and want to use an existing xib to base my layout off of. What's the best way to create the new xib file? If it was Android I'd copy and paste the xml and then start tweaking but with the IB I don't have that option (right?).
I've tried two things so far, (1) copying and pasting the file in xCode but the option isn't there and (2) copying all the UI elements from the old xib and pasting them into a new blank xib. This copies all the UI objects, but doesn't maintain structure or spacing at all. What's the best way?
Just make a copy of NIB file, rename it and add to the same Xcode project. Change File Owner class in new nib if needed.
When I need to do this I convert the file to a .nib (shift click the file and delete .xib and add .nib) then you can see all the XML copy that. Then I make a new .xib and convert that to .nib and paste the XML then you can convert them both back to a .xib. You can also Right click the file and navigate to View As > Source Code
If Storyboard is used: Add a new UIViewController to the storyboard. Remove its main view so that the view controller is empty. Set the Class of the view controller (in the Identity Inspector) to that of your nib's File's Owner. Also make sure the .h and .m files are added to your project already.
Now open the nib, select the top-most view and press Cmd-C to copy it to the clipboard. Switch to the storyboard, select that new, empty view controller. Press Cmd-V to paste the contents of the nib into this view controller.
You may have to reconnect all the outlets and actions, but at least you don't have to do the layout again.
Related
I'm trying to use this repository from Github and to use it you must have a xib file whether it's in your storyboard or not and I wanted to know how I can do to have my view controllers acting like xib but in my storyboard.
I'm sorry if I'm not clear enough but just tell me in the comments and I'll try my best to be clearer.
Kindest regards,
All you have to do is in your project select New File from the file menu or by right clicking in your file navigator. On the popupWindow select iOS on the left (This will let you see all the options) find the "View Option".
and this will create a View.Xib file. This is just the UI file for a view.
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.
I have a project that has the header and the implementaiton files and loads a plain old UITableViewController from the .m but I have no way of configuring the View (I would like to do stuff like ad buttons and text fields onto the view instead of just having a table), should I create a .xib file so that I can modify the view? (Reason I don't just start a new project is that the current project has a bunch of other stuff like OAUTH processes that kick off at the AppDelegate stage to log a user into a system - and I need this functionality). So my question how can I create a .xib from the existing .m .h files and then add other objects which I can then code for.
An answer from this similar question:
"Here's a more step-by-step way to associate your new UIViewController and .xib.
Select File's Owner under Placeholders on the left pane of IB. In the Property Inspector(right pane of IB), select the third tab and edit "Class" under "Custom Class" to be the name of your new UIViewController subclass.
Then ctrl-click or right-click on File's Owner in the left pane and draw a line to your top level View in the Objects portion of the left pane. Select the 'view' outlet and you're done.
You should now be able to set up other outlets and actions. You're ready to instantiate your view controller in code and use initWithNibName and your nib name to load it."
If it's a UITableViewController, simply create a new XIB file via Xcode (File -> New -> iOS -> UserInterface -> View) and then add set the file's owner to your subclassed UITableViewController.
You'll likely want to re-do how the user interface looks -- in terms of dropping objects like the table view and buttons or whatever else -- into the XIB's view. It'll certainly save a lot of time versus trying to debug programmatically creating and adding subviews and actions.
And once that's in place, you can then make IBOutlets and IBActions to your heart's content.
I want to remove xib file for UIViewController classes. Please let me know, If any one know how to remove or delete xib file.
You can just delete the .xib file,
If you want a new .xib for your view controller, create new xib
Then give ViewController as File's Owner's Custom Class,
Finally, link the ViewCrontroller's view and .xib
I'd like to add that the labels and everything I had placed in the file still showed up in the Simulator after I had deleted my .xib
It seems, Simulator caches the contents and I had to delete the app from inside the Simulator to force it to totally rebuild. Worked after that.
I added a custom view controller class with interface (.h), implementation (.m) files, and UI (.xib) to an existing project.
When I ctrl-click and drag between the nib and the interface, it will not place an outlet or IBAction when I drag, so I can't link a UI element to a property. Also, XCode does not bring up the corresponding interface file when I view the nib in assistant editor mode.
Note: I changed the name of the custom class. I changed the filename and the references to it in the source.
Make sure you click on file's owner on the left and then in the identity inspector on the right, put in the custom class. You should then be able to make connections.