Occasionally I like to create UIViewController files within their own .xib files, rather than within a storyboard. One example of when I favour this approach is for when I have a UIViewController that can appear above multiple different VC's within the app, such as a login screen for example. In this case its not really part of the 'storyboard' flow of an app, linked by a segue. A big advantage too is that I can create and develop my xib and be sure of no conflicts with other developers on a team, which is not the case with storyboards. You can of course help to simplify storyboards and avoid conflicts by having multiple storyboards dedicated to different areas of an app. But in the case of a login screen, theres no flow there necessarily - just a single screen I need to show anywhere. So a single .xib UIViewController is a nice easy way to go. Heres how its set up.
First, make a new UserInterface file, of type View.
Then make a new .swift file to contain the UIViewController class you are making, and add the basic methods :
Now return to interface builder to edit the new .xib file you made. First, go to Files Owner and set the class as your new view controller class (in my case, TestViewController). Tip - if you start typing the class and it autocompletes, you know you have it ready to go. Its possible to enter the class name here even if the class doesnt exist, and stuff wont work later.
Now, very important bit : set the view property on Files Owner to be the view in your xib. You do this by Ctrl-dragging from Files Owner onto the view then selecting view from the little menu that appears :
From here, you have properly set up the class, and the xib. Now you are able to do things like drag properties from your view into the UIViewController class swift file.
To instantiate this in code :
Important to note there : your new UIViewController subclass name (in my case TestViewController) is used both in the name of the call, and as a string in the nib name field. In that code I present simply by presenting on top of the current view controller. You can of course do many different things there, for example, pushing on to the current UINavigationController.
One final note - if you try and call to instantiate a UIViewController like this from within the viewDidLoad method of another view controller, it wont work. It is unable to present the view at this stage. You need to go beyond the viewDidLoad phase into viewWillAppear for eg, at which point and beyond, you can do it.
All comments welcome of course.
Subscribe to:
Post Comments (Atom)
Social Network with Firebase 3 - Infinitely scrolling UITableView of Firebase Items
Ive already talked about the basics of a Firebase social network project , and adding posts in two other articles. Here I want to explain ...
-
Ive already outlined my database structure for my Firebase based social network. Ive done the auth for new users , and Ive added each new ...
-
Im doing an awful lot of work at the moment with AVMutableVideoCompositions, and AVAssetExportSessions. When exporting a video using the la...


No comments:
Post a Comment