问题
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