简体   繁体   中英

Right way to switch between UIViews in ios programming

Hy everybody

I am a newbie ios programmer and I'm facing many doubts when I must switch the pages of my app.

With the term "page" I mean a UIView that fills the whole screen with some widgets (buttons, textboxes. tables..)

As far as I have understood what I've read I should use an UIViewController to manage each of these pages since each page should be a screen's worth of content.

My App starts with a ViewScroller with many buttons and when the user clicks one of these it opens a new page.

The first page is the UIView connected to the RootController Of the Window.

So far to open the new pages I add a child controller to the RootController and it's view as a child of the view of the RootController:

RicLocaliController = [[RicercaLocaliViewController alloc] initWithNibName:@"RicercaLocaliViewController" bundle:nil];

[self addChildViewController:RicLocaliController];

[RicLocaliController didMoveToParentViewController:self];        

[self.view addSubview:RicLocaliController.view];

RicLocaliController.view.frame = self.view.bounds;

When the user clicks the "Back" Button I remove the child controller and the child view.

Going down this road I would get a dynamic tree of Controllers with their Views.

So far I have not encountered problems and my app can go up to a third level in the tree and come back. Each page behaves correctly when orientation changes.

But I'm afraid that adding, for each subpage, a child controller and a child view could be not the right thing to do.

I'm afraid that if I nest a lot of pages when the orientation changes the app could respond slowly since also the superviews will do something to manage this event.

So what I wonder is if what I am doing is completely senseless, if I should use Navigation controllers or some other way to manage my page changes.

Unfortunately my boss is not giving me enough time to study well the subject and so I would like an advice to follow the best solution possibly using the most standard and less complex component offered by the framework instead of the newest features.

I read a lot of web pages on the subject but it seems to me that there are many ways to manage the navigation beetwen pages and this makes me confused.

I apologize for my bad english but i'm tired and English it's not my first language.

You HAVE to do some studying. You will spend more time clearing up all your problems later otherwise... but, here are some tips.

Using nested ViewControllers leads to all kinds of trouble so if you are short of time, skip that.

Think of each "Page" as one ViewController. A ViewController has a property called View but that is actually just the top view of a whole hierarchy of views. A view is the base class for any visual object, like labels, buttons etc. All views can have subviews, so you can add an image under a label etc. and do really wierd stuff if you want to. I am just saying this to free your mind about how you can use views.

Now, ViewControllers are supposed to hold to code to ONE view hierarchy. That view hierarchy is for that View Controller only.

When the user wants to navigate to another page, you have a few alternatives:

NavigationViewController - that should be used when the user wants to delve down into data, like opening a detailed view of an item in a list etc. The NavigationViewController gives you help with back buttons, proper animation etc. You "pop" a viewcontroller to go back one level. If the user click the back-button, this is automatic.

TabBarViewController - use that if you want a tab bar at the bottom of the screen. Each tab is connected to a ViewController, that has it's own view hierarchy.

PushModal - If you are in a ViewController and just needs to get some data from the user, which is not part of the normal navigation of the app, you can push a new ViewController modally. This is the way you interact with iOS built in ViewControllers. This is also a good way to get a value back from the view controller.

There you have it. Go learn more. :)

It sounds like, for what you are using, you should be using a navigation controller. This will automatically handle pushing views onto the stack and then popping them off again later. This will also automatically create a back button (it is customizable) in the navigation bar.

If you are using iOS 5 or 6, I highly recommend trying out "storyboards" in Interface Builder. Storyboards allow you to graphically represent transitions (called "segues") between different views.

On top of being easier to design and implement, another advantage is that, if in the future you want to change the design of your application, you don't have to trawl through all your code and manually update each view connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM