To use presentModalViewController I must alloc and initWithNibName:bundle: a view. My question is: where do I release it? Does the dismissModalViewController:animated: handle the release?
No. If you allocatet and initialized it, than you have to release it. Do it like this:
YourViewController *controller = [[YourViewController alloc] initWithNibName:#"YourView" bundle:nil];
[self presentModalViewController:controller animated:YES];
[controller release];
PresentModalViewController:animated: increases automatically the retainCount. And as soon as the controller disappeared from the view, it will release the controller. And so the controller will be deallocated. ;-)
You can release after you present the controller; the presenting controller (its parent) will retain your modal controller.
Related
I have a UIViewController class and a second which I want to push modally preferably. However I can't seem to call [self pushModalViewController:...], how come?
What requirements do I need to meet to be able to do so?
I am doing this and getting a black view pushed:
vc = [[ViewController alloc] init];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
I have made my view controller in my storyboard and given it a custom class. I am trying to present this view modally via this class as seen in my code.
Any help much appreciated, thanks.
'push' and 'modal' don't belong together in the same thought. You can:
present a modal view controller, preferably using -presentViewController:animated:completion:, which is the modern replacement for -presentModalViewController:animated:
push a view controller onto the navigation stack, assuming that you're using a UINavigationController. To do that from a view controller, use:
[self.navigationController pushViewController:foo animated:YES];
You're actually looking for [self presentModalViewController:myViewController.view animated:YES]
How are you initing this viewcontroller. From the posted code, I assume it is being initialized to a blank view.
Maybe you can init it from a nibname or something?
vc = [[ViewController alloc] initWithNibName:#"NibName" bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
Also another thing to note, if this is a custom class and no nib file, is there any code in initWithCoder? How does the viewDidLoad looks like?
I have some pretty simple code where I am using a UINavigationController and adding a rootViewController. After some processing has occurred I want to pop off the current view controller and replace it with another one. This seems to work fine, but my original view controller does not dealloc. I set a breakpoint in it's dealloc and it never gets hit. Below is my code. Not sure why happens. Just for testing if I release startController twice it does go away.
StartViewController *startController = [[StartViewController alloc] initWithNibName:#"StartViewController" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:startController];
[nav pushViewController:startController animated:NO];
self.navController = nav;
[startController release];
[nav release];
Thanks any help.
Your ultimate goal is to bring the view controller's retain count to zero. So make sure that everything the view retains is released and anywhere the view is retained also release.
Please check the following possible causes:
Make sure you pop the view controller from the navController if you have a custom back button. The default Back button will work fine.
Make sure that all your IBOutlets are set to nil in viewDidUnload
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
}
If your view is an observer to a model class to receive events
For example
model.addObserver(myView);
and sure to also do
model.removeObserver(myView);
I hope this helps.
It looks as though your self.navController has got a holding reference to it. maybe put
self.navController =nil;
somewhere appropriate, so that when the view has been popped it is released.
I was trying to pop off the root view controller. I instead used the setViewControllers message from the UINavigationController object to replace all my view controllers.
MyPlatesViewController* viewController = [[MyPlatesViewController alloc] initWithNibName:#"MyPlates" bundle:nil ];
[self.view addSubview:viewController.view];
then i delete my viewController
[self.view removeFromSuperview];
but leak instrument shows 20 MB memory
What is wrong ?
You leaked the view controller object. After you remove the view from its superview, you need to release the controller as well.
Alternatively, you can do the following:
[self presentModalViewController:viewController animated:NO];
[viewController release];
Then, when dismissModalViewController is called, both the view and the view controller will be released properly.
You called alloc so it's your responsibility to release it. Your code should look like this:
MyPlatesViewController* viewController = [[MyPlatesViewController alloc] initWithNibName:#"MyPlates" bundle:nil ];
[self.view addSubview:viewController.view];
[viewController release]
Note that your controller is retained by the view when you call addSubview and released when you call removeFromSuperview. So with your current code the retain count of viewController is still 1 after calling removeFromSuperview.
Additionally you should review objective-c memory manament here: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html
recently I joined two Xcode projects together. To get this thing to work, I had to alloc and initialize my view controller.
self.myViewController = [[MyViewController alloc] init];
But why? In the other project I have the same code. The sole difference is hierarchy of the different views. I added a new view to the top (beginning). So the calling View Controller is not the first view on the stack anymore.
I'm pushing my view in this way on the stack:
[[self navigationController] pushViewController:myViewController animated:YES];
In my NIBs I have added a View Controller object with IB and connected the Outlets.
And I have a memory management question too: If I have a property like myViewController, do I have to release it? The "normal" release is done in the dealloc method. But do I have to use an additional release because of the alloc? I don't think so, but I ask you anyway.
I would need to see more code to answer why you had to alloc your view controller, but I'd say that you always alloc them manually (at least in my experience).
As for the memory management question, if your property is declared as a retain property (#property(retain) UIViewController *myViewController), you are indeed leaking memory, since the retain count after alloc will be 1, and after the retain done by your accessor will be 2. Hence, if you release it only once, you'll end up with a leak.
I usually do this instead:
self.myViewController = [[[MyViewController alloc] init] autorelease];
I found it out: In IB I had to set the nib name on my view controller object. So the alloc and initialization is done by IB?
There is one more option:
(IBAction)loginButton:(UIButton *)sender {
NSLog(#"pressed login");
ICMasterViewController *controller = [[self storyboard] instantiateViewControllerWithIdentifier:#"mainnav"];
[self presentViewController:controller animated:YES completion:nil];
}
On your storyboard you must have UIViewController with name mainnav
I have a UIViewController that I wish to push onto the stack, however, when I call [viewController release] on it after I push it, any time I pop it off of the stack however, I get various errors pertaining to deallocated instances of the view controller. Here is an example of an implementation:
RootViewController *rootViewcontroller = [[RootViewController alloc] initWithNibName:#"RootViewController"
bundle:nil];
[self.navigationController pushViewController:rootViewController animated:YES];
[rootViewController release];
In the code that you showed you create rootViewController but actually push personsViewController. Typo? Or did you paste the wrong code?
Your RootViewController instance is "rootViewcontroller" but you're pushing "rootViewController" (notice the 'C'?).