how to retain view after addSubview of UIViewController with ARC

自作多情 提交于 2020-01-02 13:28:09

问题


How to handle situation when I use ARC and add view of UIViewController?

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[someView addSubview:vc.view]; //this retain vc.view

because addSubview retain onlu view, not controller, so controller is released. Before ARC there was a way to retain controller as long as neede, but how to prevent ARC to release View Controller?


回答1:


I had similar situation solved by declaring vc as property with default strong attribute.




回答2:


#define AntiARCRetain(...) void *retainedThing = (bridge_retained void *)__VA_ARGS; retainedThing = retainedThing

And then call AntiARCRetain(controller);




回答3:


Why would you need a new ViewController there?
You should just add your View as Subview and handle everything with the ViewController of "someView"



来源:https://stackoverflow.com/questions/9144959/how-to-retain-view-after-addsubview-of-uiviewcontroller-with-arc

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