开发者

Managing multiple UIViews from one UIViewController

开发者 https://www.devze.com 2023-01-18 19:03 出处:网络
I\'m getting confused on view controllers and would love a straight example. Here\'s the preamble: I have a UIViewController with a matching .xib.

I'm getting confused on view controllers and would love a straight example. Here's the preamble:

I have a UIViewController with a matching .xib. By default IB gives me a single View in the Document window. I can make it appear by telling my UIWindow to addSubview:controller.view开发者_开发技巧 and bringSubviewToFront:controller.view

Here's the questions:

  1. Should I add another View to the ViewController in IB? Or is there a better, programmatical way?

  2. How do I tell the ViewController to switch between the Views?

  3. From the ViewController downward, what does the code look like to achieve this?

I'm trying things but just making a mess so I thought I'd stop and ask...


Note that every button, label, image, etc. in your main view controller is actually a view in itself, however I've interpreted your question to mean that you want to manage multiple full-screen views or "screens". Each screen should have its own view controller to manage it. So to get the terminology right, a view-controller is an object that manages a single full-screen view (or almost full screen if it's nested inside a navigation controller or tab bar controller for example) and a view is the big area managed by the view controller as well as all the sub-views (images, buttons, labels, etc.) within it (they are all UIView sub-classes). The view controller manages all of them on that screen, if you want another screen/page then you should create a new view controller to manage it.

The root view controller (the one you add to the window) can be a plain old normal view controller that you've designed in IB, however it's probably more useful if you use a navigation controller or a tab bar controller and add your designed view controller to that - then you can push additional view controllers as needed.

Another way (if you don't want navigation or tab-bar style) would be to transition to other view controllers directly in the main window using whatever transitions you like (or just replace the old one). We'll leave that for now though.

Any sub-views of your main view controller (the one you've designed in IB) will be automatically loaded from the nib file, but you can also add your own views programatically if you want (typically you would use one or the other, i.e. nibs or programatically, but you can mix and match if you want). To do it programatically, override loadView in the view controller and then call [super loadView]; then do [self.view addSubView:myOtherView]; (create the myOtherView first of course). Note that the first time .view is accessed on your view controller, it actually calls loadView to create the view, so inside loadView it's important to call [super loadView]; before trying to access self.view :D

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

- (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 about view controller is created programatically here - if your about view is designed in IB then instead use initWithNibName:bundle: to create it.

And that's how you manage multiple screens.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号