2

In an application where several UIViewControllers work together,

firstViewController added to root. Till here its fine now I want to go to secondViewController I dont want to use UINavigationController or UITabBarController. I have already read the View Controller Programming Guide but it does not specify without using UINavigationController, UITabBarController and story board.

and when user want to move from secondViewController to firstViewController how secondViewController will be destroyed?

Apple Doc also does not specify how UIViewController is release or destroyed? It only tell the life cycle inside UIViewController.

S.J
  • 3,063
  • 3
  • 33
  • 66
  • @MartinR I would like to invite you to answer this question. – S.J May 02 '13 at 08:42
  • Replacing the window's root view controller as described e.g. here: http://stackoverflow.com/questions/16334041/re-assigning-rootviewcontroller-after-successful-login might be an option. – Martin R May 02 '13 at 09:14
  • you should use iOS 6 feature "childViewControllers" of UIViewController class, read this: Implementing a Container View Controller @ http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html – art-divin May 02 '13 at 09:27
  • @MartinR transitionfromview method only play with view it do not manage viewcontroller, and is it a good practice to replace root view every time? – S.J May 02 '13 at 09:50
  • @MartinR if I want to destroy/release an Instance of UIViewController, how to do it in both arc & non-arc and is it a good practice? – S.J May 02 '13 at 09:57
  • @S.J: The first part of your last question (destroy/release in arc/on-arc mode) is quite broad and impossible to answer in a comment. - I cannot give you advice on "good practice" here, it probably depends what you are using it for. "Switching root view controllers" and all the solutions suggested in the answers seem sensible to me. – Martin R May 02 '13 at 10:07
  • @MartinR Please write an answer regarding destroy/release in arc/on-arc mode. – S.J May 02 '13 at 10:10
  • @MartinR here you can answer Please view http://stackoverflow.com/questions/16335537/what-are-all-the-possible-ways-to-jump-from-one-view-controller-to-another-and-b – S.J May 02 '13 at 10:18

4 Answers4

2

If you are concerned about how UIViewController is release or destroyed then here is a scenario for you:-

Here is a button tap method in FirstViewController that presents SecondViewController (using pushViewController,presentModalViewController etc)

In FirstViewController.m file

- (IBAction)btnTapped {

    SecondViewController * secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
    NSLog(@"Before Present Retain Count:%d",[secondView retainCount]);
    [self presentModalViewController:secondView animated:YES];
    NSLog(@"After Present Retain Count:%d",[secondView retainCount]);
    [secondView release];  //not releasing here is memory leak(Use build and analyze)
}

Now In SecondViewController.m file

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"View Load Retain Count %d",[self retainCount]);
  }

- (void)dealloc {
    [super dealloc];
    NSLog(@"View Dealloc Retain Count %d",[self retainCount]);
 }

After Running the code:

Before Push Retain Count:1
View Load Retain Count 3
After Push Retain Count:4
View Dealloc Retain Count 1

If you are allocating and initializing a ViewController, You are the owner of its lifecycle and you have to release it after push or modalPresent. In the above output at the time of alloc init retain count of SecondViewController is One,,,,surprisingly but logically its retain count remains One even after it has been deallocated (see dealloc method), So require a release in FirstViewController to completely destroy it.

gagan sharma
  • 256
  • 1
  • 4
  • 18
  • Please can you also answer this question http://stackoverflow.com/questions/16335537/what-are-all-the-possible-ways-to-jump-from-one-view-controller-to-another-and-b – S.J May 02 '13 at 10:41
  • I didn't find your question,,,voluntarily removed by its author ! – gagan sharma May 02 '13 at 11:23
  • I removed it :( ... I asked that I want to know all the possible ways to jump from one VC to another. 1> NavigationController, 2> UITabBarController, 3> presentModalViewController:, 4> removing old VC'c view and alloc init view VC & assign to root view. Is there any other possible way? – S.J May 02 '13 at 11:41
  • Yes,transitionFromView: presents views with different animations,but you can do the same using view-controller.view,,,and all possible ways to jump from one VC to another you have described...@# – gagan sharma May 02 '13 at 13:07
  • please view this question http://stackoverflow.com/questions/16337706/need-assistance-regarding-transitionfromviewtoviewdurationoptionscompletion/16338755?noredirect=1#16338755 – S.J May 03 '13 at 05:23
  • I would like to invite you to answer this question http://stackoverflow.com/questions/17657719/please-clear-some-confusions-regarding-uiviewcontroller – S.J Jul 16 '13 at 05:24
1

Other way to present a new view controller is doing it like a modal view controller(notice that self is firstViewController):

[self presentModalViewController:secondViewController animated:YES];

then, when you wan to come back to the firstViewController and destroy the secondViewController, you have to dismiss the view controller(from the secondViewController):

[self dismissModalViewControllerAnimated:YES];

Hope that helps.

  • sorry I also forget to mention model view presentation, I also dont want to use this. In games where there is no navigation or tab bar controller or model view presentations. How they jump from one view controller to another. – S.J May 02 '13 at 09:25
  • you can have animated property as NO then – Firdous May 02 '13 at 09:34
  • @HéctorDarioArroyoGóm I would like to invite you to answer this question http://stackoverflow.com/questions/17657719/please-clear-some-confusions-regarding-uiviewcontroller – S.J Jul 16 '13 at 05:25
1

You can use UINavigationController to move to secondViewController and come back by setting the UINavigationController property 'navigationBarHidden' to YES. Which will hide the navigation bar. Releasing and destroying of view controllers will takes care by this.

Prasad Devadiga
  • 2,573
  • 20
  • 43
0

Then, you can take other strategy, is not the best to build your view controller hierarchy, but it also could work. You can overlay the secondViewContrller's view over the firstViewController's and make the secondViewController become the child from the firstViewController:

//...
[self addChildViewController:secondViewController];
[self.view addSubview:secondViewContrller.view];
//...

And when you want to remove the view controller, you have to remove the view and ask for the view controller to remove from his parent:

//...
[self.view removeFromSuperview];
[self removeFromParentViewController];
//...

But then you will have to control the view hierarchy by your own(putting and removing views and view controllers by your own).