Objective-C - Understanding view controllers
I understand that view contr开发者_运维问答ollers help control multiple views in an application, but I have trouble understanding when to use them.
If I have an application with a main page, several views with a "hierarchy" structure, and an about page not connected with the hierarchy, what files should my application have? An appdelegate, navigation controller and view controller? More than one view controller? Just a navigation controller?
Also, should they all be contained in one .xib file, or multiple .xib files?
Any help would be greatly appreciated.
Thanks.
A good habit is to have a UIViewController
for each page you want to show. If I get the structure of your app you should have a main page (with many other UIView
s inside it) and another page (about page). If that's true I suggest two UIViewController
s.
The UINavigationController
is a subclass of UIViewController
that lets you "navigate" among the pages. It's not strictly necessary but suggested (you can also implement your self a custom navigation system, but it's easier to exploit the one Apple offers you). Another navigation system is the one based on UITabBarController, if you want to take a look.
Assuming I get the structure of your app you should need two .xib file, one for each page you have.
The app delegate is conceptually different from a view controller, you'll have just a single app delegate, automatically created by Xcode (you can, of course, modify it to fit your needs).
Each "screenful of content" (Apple uses this term) should be handled by it's UIViewController
or more likely a subclass of it. The point of view controller is to handle view appearing or disappearing (going on/offscreen), device rotation, memory management, navigating to other view controllers and so on. If you are creating your UI with IB, then each of those view controllers would most likely have it's own .xib file.
Each view controller has one view (it's view
property) that acts as main view for each "screenful of content" to which you then add your subviews.
UINavigationController
and UITabBarcontroller
are there to help you control the hierarchy of your app. They only act as containers for other view controllers and don't contain any UI except navigation bar or tab bar. Using tab bar controller you can have multiple view controllers which act exactly like browser tabs. Using navigation controller you can have a stack-like navigation where new view controllers are pushed from right to left and are popped from left to right when user goes back to previous view controller. You can even have a view controller inside navigation controller inside a tab bar controller.
If you don't want to use tab bar or navigation controller, you can navigate through your view controllers by presenting them modally using presentModalViewController:animated:
and dismissing by dismissModalViewControllerAnimated:
. If you send YES
for animated parameter of these methods, you will get an animation specified by the modalTransitionStyle
property of view controller being presented or dismissed. Possible animations are slide in from bottom (default), horizontal flip of entire screen, fade in/out and half-page curl.
There are also some Apple-provided subclasses of UIViewController that help you setup your UI quicker like UITableViewController
which is basically a view controller that contains a table as it's main view and conforms to 'UITableViewdataSource
and
delegate` protocols which are required to define how each cell looks and what it contains.
On iPad there is one additional container controller UISplitViewController
and one additional way to present new view controllers using UIPopover
.
精彩评论