How to correctly switch view?
i know its already allot of info about that in internet, but i'm new to programming and little confused, i need little help please... for example i have 10 views controllers and switching between it with a buttons for example i switching with
-(IBAction)goToSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentModalViewController:secondView animated:NO];
[secondView release];
}
/* or */
-(IBAction)goToSecondView:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc]
initWithNibName:@"SecondViewController" bundle:nil]; [self.view addSubview:secondView.view];
[secondView release];
}
and after need if i switch to remove the first view with that:
[self.view removeFromSuperview]; /* or */ [self dismissModalViewControllerAnimated:YES];
and i dont know how to make remove the first view when i switching to second, for example when go from FirstView to SecondView the second view, need to remove the FirstView and after from SecondView go to ThridView开发者_开发知识库 or to SevenView example and need to remove the SecondView... how can i do that?can you make a sample code please? i know its very easy but not for me, im new to programming
--------------------update-----------------------------------------------------------------
i want make something like > here < but i dont understand all, can any one make a sample code please?.
Thank You Very Much
Which of those methods you use (or if you should use another) depends exactly on what you are trying to achieve.
If there was one view which listed all the other ones and all you wanted to do was go back and forth between the first view and another one then the presentModalViewController:otherView approach would be fine. If you are adding the other views as a subview of the first ones and completely covering it up, that is not the best way to do things. You should rather add the other view as a subview of the window (or the other views parent view) and then remove that view from the window/parent view using [theView removeFromSuperview];
If you have a hierarchical structure, i.e. you can go several layers deep, from the first view to the second, from it to the third, then you should look at using a UINavigationController. Each time you want to show a new view you push it onto the UINavigationController's stack, to go back a level you pop it off (if you use the default navigation bar with a back button this is taken care of for you). In this situation you don't have to deal with removing unseen views and recreating them when needed, thats all taken care of by the UINavigationController.
Remember also that you needn't always 'remove' views. You can hide them instead.
[self.view setHidden:YES]
This will just make them not be drawn. That's handy, because removeFromSuperview will actually release the view from memory unless it's been retained, meaning you'll never get it back.
精彩评论