Need assistance regarding UIViewController

不问归期 提交于 2020-01-24 19:28:52

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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).



来源:https://stackoverflow.com/questions/16333731/need-assistance-regarding-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!