cyclingIsBetter
cyclingIsBetter

Reputation: 17591

IOS: release viewcontroller in storyboard

I have this code to open a view controller with storyboard

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    secondViewController = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.view addSubview:secondViewController.view];

and it work fine, but when I remove it I want release secondViewController

[secondViewController.view removeFromSuperview];

and for release it??? there isn't an alloc when I call secondViewController...

Upvotes: 0

Views: 2936

Answers (3)

Stavash
Stavash

Reputation: 14304

First of all, it's important to make the distinction between your visual UIView instance and the UIViewController instance. The instantiateViewControllerWithIdentifier call returns a view controller object that has a UIView property (the UIViewController doesn't need to be explicitly released as it is an autoreleased instance) - this UIView is what's being added to a superview and it's also being retained by it, as long as it is indeed a subview of some view. Once you remove that view from the superview and provided you're using ARC, the view should be released unless you're holding a strong reference to it's UIViewController, in that case just assigning the UIViewController property a nil value will take care of it. If ARC is not used, you need to call "release" only if you explicitly retained the view controller (not the view)

Upvotes: 0

graver
graver

Reputation: 15213

-instantiateViewControllerWithIdentifier: returns an autoreleased object. You don't have to release it. When you remove its view from its superview it will be released. If you are using ARC this question shouldn't be asked at all...

Upvotes: 2

Levi
Levi

Reputation: 7343

I assume you are using ARC. Just write secondViewController = nil;. It should be released automatically.

Upvotes: 0

Related Questions