需要有关UIViewController的帮助 [英] Need assistance regarding UIViewController
问题描述
在几个UIViewController一起工作的应用程序中,
In an application where several UIViewControllers work together,
firstViewController
已添加到根目录.到这里为止一切正常,现在我要转到secondViewController
,我不想使用UINavigationController
或UITabBarController
.我已经阅读了《查看控制器编程指南》 ,但是如果不使用UINavigationController, UITabBarController and story board
,则不会指定.
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
.
,以及当用户想要从secondViewController
移至firstViewController
时,如何销毁secondViewController
?
and when user want to move from secondViewController
to firstViewController
how secondViewController
will be destroyed?
Apple Doc也没有指定如何释放或销毁UIViewController?它只能说出UIViewController
内部的生命周期.
Apple Doc also does not specify how UIViewController is release or destroyed? It only tell the life cycle inside UIViewController
.
推荐答案
如果您担心 UIViewController 的发布或销毁方式,那么可以使用以下方案:-
If you are concerned about how UIViewController is release or destroyed then here is a scenario for you:-
这是FirstViewController中的按钮点击方法,其中显示 SecondViewController (使用pushViewController,presentModalViewController等)
Here is a button tap method in FirstViewController that presents SecondViewController (using pushViewController,presentModalViewController etc)
在FirstViewController.m文件中
- (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)
}
现在在SecondViewController.m文件中
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"View Load Retain Count %d",[self retainCount]);
}
- (void)dealloc {
[super dealloc];
NSLog(@"View Dealloc Retain Count %d",[self retainCount]);
}
运行代码后:
推入保留计数前:1
查看负载保留计数3
推后保留数:4
查看取消分配保留计数1
Before Push Retain Count:1
View Load Retain Count 3
After Push Retain Count:4
View Dealloc Retain Count 1
如果您要分配和初始化ViewController,则您是其生命周期的所有者,必须在 push或modalPresent 之后释放它. 在上述 alloc init 时的输出中,SecondViewController的保留计数为1,令人惊讶的是,但是从逻辑上讲,即使已将其保留,保留计数仍为1(请参见dealloc方法),因此在FirstViewController中释放以完全销毁它.
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.
这篇关于需要有关UIViewController的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!