将ViewControllers的视图添加为subView [英] Adding a ViewControllers's view as subView
问题描述
我正在将ViewControllers视图作为子视图添加到另一个ViewController中,
I am adding a ViewControllers view as a subView to another ViewController,
示例:
在FirstScreen ViewController中,我这样做,
In FirstScreen viewcontroller i do this,
[self.view addSubview:self.secondScreen.view];
一旦我将其从FirstScreen中移除,我就这样做
And Once i remove it from FirstScreen i do this
[self.secondScreen.view removeFromSuperView];
self.secondScreen=nil;
但是在添加subView时调用 ViewDidLoad 方法,而在调用removeFromSuperView ViewDidUnLoad 时不调用.
But While Adding the subView ViewDidLoad method is called but while removeFromSuperView ViewDidUnLoad is not called.
我的问题
1)一旦我将实例self.secondScreen设置为nil,我的secondScreen中的所有对象都将被释放.
1) will all my objects in my secondScreen will get deallocated once i set the instance self.secondScreen to nil
2)这样安全吗,这样我就不会收到任何泄漏或内存警告
2)Is it safe to do like this so that i wont get any leaks or memory warning
推荐答案
假设您的应用从iOS 5.0开始支持,您需要将其添加为
Assuming that your app supports from iOS 5.0 onwards, you need to add it as,
[self addChildViewController:self.secondScreen];
[self.view addSubview:self.secondScreen.view];
类似地,您可以使用 removeFromParentViewController
进行删除,然后从超级视图中删除.检查
Similarly for removing you can use removeFromParentViewController
and then remove from superview. Check apple documentation here.
ViewDidUnLoad
is deprecated from iOS 6.0 onwards and will not get called. Check the documentation here.
关于您的问题,
1)一旦我将实例self.secondScreen设置为nil
1) will all my objects in my secondScreen will get deallocated once i set the instance self.secondScreen to nil
完成 self.secondScreen
类后,将其设置为nil后,它将开始释放该类内的对象.如果您使用的是ARC,则不必担心发布.操作系统会处理这些事情.
Once you are done with self.secondScreen
class, it will start releasing objects inside this class once you set it to nil. If you are using ARC, you dont have to worry much about releasing. OS will take care of those things.
2)这样做是安全的,这样我就不会有任何泄漏或记忆警告
2)Is it safe to do like this so that i wont get any leaks or memory warning
是的,如果您使用的是ARC,这很好.对于非ARC,您需要确保已正确释放此类中的所有变量.在这种情况下,请确保保留/释放全部平衡.
Yes, this is fine if you are using ARC. For non-ARC, you need to make sure that you have released all variables properly in this class. Make sure the retain/release are all balanced in that case.
这篇关于将ViewControllers的视图添加为subView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!