0

I have an application with many views. Whenever I click the next button on the first view I present the second view. Currently I am doing this as follows:

NextView *second = [[NextView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];

In this application, however, I don't need to keep any information from the first view any more. Is there some way I can bring up the second view, and deallocate the first view from memory? I was thinking that if I bring up 5 views in a row, and all of them are modal, then I will be using more memory that I really need. If the user hits the back button, I would also like to open the first view, and remove the second from memory.

Is there some way to do this to save memory, or is this memory just something I should not worry about?

  • Did you look at `UINavigationController` ? – Martin R Aug 13 '12 at 05:46
  • How can you change your view with nibName nil .... And also you are not assigning any frame to it.... What do you think ? Will it take its frame automatically ????? – TheTiger Aug 13 '12 at 05:47
  • @VakulSaini: You can use `initWithNibName:nil`. The nib name is then derived from the view controller's class name. – Martin R Aug 13 '12 at 05:50
  • @MartinR - Yes i know we can ..... in this case we use init method of viewController class to assign frame... May be i'm confused because of less info of question.... :-) – TheTiger Aug 13 '12 at 05:55
  • @MartinR I was hoping I could just open the next view in some non modal fashion. Doesn't the previous view still get retained in memory when the next view is displayed when using the UINavigationController? Instead of push and pop, I was hoping for display new view and blow away old view. – user1594454 Aug 13 '12 at 06:42

3 Answers3

0

You should definitely try to use UINavigationController. With pushViewController:animated: you push a new view controller on the stack, and you get a navigation bar with a "Back" button for free. With popToViewController:animated: you can go back to the initial view controller.

UINavigationController is very well documented and there are lots of sample programs. I haven't used Storyboard yet, but that should make it even simpler to build a navigation controller based sequence of views.

You need not care about releasing the views from memory. Views are unloaded automatically if memory is low. If your view controller allocates huge amounts of additional memory, then you should implement didReceiveMemoryWarning to release that memory. See The View Controller Life Cycle for details.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

I think you should use navigation bar and hide it and use the answer here marked correct.

iPhone - Replacing rootview in navigation controller

Community
  • 1
  • 1
Mahmoud Fayez
  • 3,398
  • 2
  • 19
  • 36
-1

While going to next view,add release like

NextView *second = [[NextView alloc] init];
[self presentModalViewController:second animated:YES];
[second release];
Madhumitha
  • 3,794
  • 8
  • 30
  • 45