Manage multiple UIView from one UIViewController

I am confused about view controllers and would like to get a direct example. Here's the preamble:

I have a UIViewController with the corresponding .xib. By default, IB gives me one view in the Document window. I can do this by pointing my UIWindow to addSubview:controller.view and bringSubviewToFront:controller.view

Here are the questions:

  • Should I add another view to the ViewController in IB? Or is there a better software way?

  • How to make ViewController switch between views?

  • From the ViewController down, what does the code for this look like?

I'm trying to do something, but just a mess, so I decided to stop and ask ...

+4
source share
1 answer

Please note that each button, shortcut, image, etc. in your main view controller, it is actually a view, but I interpreted your question as meaning that you want to control multiple full-screen views or "screens." Each screen should have its own view controller to manage it. Thus, in order to get the correct terminology, the view manager is an object that controls one full-screen view (or an almost full screen, if it is embedded in the navigation controller or the tab bar controller, for example), and the view is a large area controlled by the view controller. as well as all sub-views (images, buttons, labels, etc.) inside it (all subclasses of UIView). The view controller manages all of them on this screen, if you want a different screen / page, then you must create a new view controller to manage it.

The root view controller (the one you add to the window) can be a regular old regular view controller that you developed in IB, but it is probably more useful if you use a navigation controller or a tab bar controller and add your configured view controller to to this - then you can click additional view controllers as needed.

Another way (if you do not want navigation or a tab style) is to switch to other view controllers directly in the main window using any transitions that you like (or just replace the old one). Until we leave it.

Any submenus of your main view controller (the one you created in IB) will be automatically loaded from the nib file, but you can also add your own views programmatically if you want (usually you should use one or the other, i.e. nibs or programmatically, but you can mix and match if you want). To do this programmatically, override loadView in the view controller, and then call [super loadView]; , then do [self.view addSubView:myOtherView]; (create myOtherView first). Note that the first time you access .view on your view controller, it actually calls loadView to create the view, so it’s important to call [super loadView]; inside loadView [super loadView]; before trying to access self.view : D

To switch between views, using the navigation controllers or the tab bar makes it very easy. So put your main view controller inside (for example) the navigation controller and put the navigation controller in the window, so that you have a window-> navigationController-> myController. Then from the action method in your view controller (you can connect the action methods in IB), for example, when the "about" button is pressed, do the following:

 - (void)doAbout { // Create the about view controller AboutViewController* aboutVC = [AboutViewController new]; // Push the view controller onto the navigation stack [self.navigationController pushViewController:aboutVC animated:YES]; [aboutVC release]; } 

Note that the view controller is created programmatically here - if your view is created in IB, use initWithNibName:bundle: instead to create it.

And how do you control multiple screens.

+7
source

All Articles