iPhone App - dismissing modal view controller doesn't dealloc it - iphone

I have a UIViewController (call it NumberTwo) which I presented as a modal view controller from another UIViewController (call it NumberOne). NumberTwo contains a touchesBegan method which listens for touches, and it also has an accelerometer method which listens for device orientation changes in the x, y, or z direction. NumberTwo has a button called "Done" which, when tapped, dismisses itself as a modal view controller:
[self dismissModalViewControllerAnimated:NO];
But it seems as though it's still listening for touches, and it's still listening for accelerations. How can I completely free up NumberTwo when I dismiss it? I tried adding a release call as follows:
[self dismissModalViewControllerAnimated:NO];
[self release];
but that caused a EXEC_BAD_ACCESS.

Did you release the controller after you presented it? E.g. in your method in NumberOneController that presents it, do you have something like:
NumberTwoController * controller = [NumberTwoController alloc] init];
// do stuff to config controller
[self presentModalViewController: controller];
[controller release];
Unless you want to hang on to NumberTwoController for re-use, this would be the usual pattern. The presentModalViewController method ensures that the controller is retained while it's in use. It should then get tidied up when, within NumberTwoController, you call [self dismissModalViewControllerAnimated: NO].

I had a very similar issue that plagued me for days. It turned out that my view controller class wasn't being deallocated when I dismissed it because that view controller had an active NSTimer that wasn't being invalidated (stopped). I was able to kill the timer in viewDidDisappear.

Make sure you are releasing everything you use when you finish with it; The dealloc method is only called when the UIViewController and all of its properties/objects are no longer in use. Never use [self release]; you need to release it from the view controller that created it after you are finished with it.

Related

placing popViewControllerAnimated: in viewDidLoad or viewDidAppear will not work

I have a view (viewB) that is pushed in using the navigation controller from another view (viewA) using pushViewController as usual, however for some reason, I want viewB's controller to pop the view using [self.navigationController popViewControllerAnimated:YES]; from inside its viewDidLoad method or viewDidAppear method, but none of them works, i.e. nothing happens (there is no crash in the app), however, i have a UIButton in viewB with IBAction that simply calls [self.navigationController popViewControllerAnimated:YES]; if the button tapped it will work and the view is popped off to the previous view !! this IBAction works if I removed [self.navigationController popViewControllerAnimated:YES]; from viewDidLoad or viewDidAppear methods because the popping will release the current view and all of its sub-views from memory.
the question is how can get the current view (viewB) to be popped off to the previous view (viewA) from inside viewDidLoad or viewDidAppear methods ?
thanks you so much in advance.
Try making method
- (void)popSelf {
[self.navigationController popViewControllerAnimated:YES];
}
In viewDidAppear add
[self performSelector:#selector(popSelf) withObject:nil afterDelay:0.0f];
This will add selector to runloop, so it will be performed after viewDidAppear.

Releasing UIViewController when not in use

Hi I hope somebody can help me with this problem.
I have a UIViewController named "Login" (for example) and when the user has successfully logged in this will call another controller to replace the "Login" controller like below:
[self presentModalViewController:anotherController animated:YES].
I do not need the previous controller any more so I placed a [self release] as shown in the code snippet below.
LoginController.m
- (void)viewDidDisappear:(BOOL)animated {
[self release];
}
This will then call the LoginController's dealloc method and I can be sure it's released.
Now in the new controller that is now in view has a button which calls a UINavigationController like below:
[self presentModalViewController:settingsNavigationController animated:YES];
But this crashes the app which would normally work if I didn't release the previous LoginController.
There is probably an easier or more logical method to release the controller but as I am running out of ideas I sometimes use drastic measures.
Thank you.
You should not be releasing the LoginController, at least not in its own -viewDidDisappear:. That controller is still in use and can be referenced, for example by the navigation controller's parentViewController property. Release the controller when it is no longer part of your view controller hierarchy, not just when it is no longer visible.
In addition [self release] is a warning sign that you are applying incorrect memory management.
1) Replace the "Login" controller
presentModalViewController doesn't replace your login view controller, but it puts anotherController on top of your login view controller.
2) viewDidDisappear
You should read documentation. Quote - You can override this method to perform additional tasks associated with dismissing or hiding the view. If you override this method, you must call super at some point in your implementation.
In other words, you must call [super viewDidDisappear:animated] too.
3) Memory Management
You should definitely need to read Memory Management Guide - http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/MemoryMgmt/MemoryMgmt.html
4) View Controller
You should definitely need to read View Controller Programming Guide too - http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Two problems with what you are doing.
First, viewDidDisappear is NOT a destructor. Do not release self there. viewDidDisAppear and appear are used for visibility of the view, not in/out of memory.
Second, presentModalViewController is intended for presenting a MODAL view controller, ie child.
You should consider setting up a UINavigationController and calling
[navigationController popToRootViewControllerAnimated:FALSE];
[navigationController pushViewController:(UIViewController*)controller animated:TRUE];
also in some situations you can get away with an [autorelease] view controller using present modal.

UINavigationController and viewWillDisappear

So I have a UINavController in my app and am trying to execute a method when the user presses the back button. I have searched everywhere and can only find bits and pieces that don't really make sense out of context.
Is there a way to implement some sort of check that catches when the user presses the back button to dismiss the current view? (the viewWillDisappear method for the view being popped never gets called for some reason. I did read that it doesn't unless you forward that call?) Does that sound right, and does anyone have any ideas or suggestions? Thanks in advance.
Take a look at the UINavigationControllerDelegate. There are the only two methods that get called when a UIViewController is pushed to the navigation controller stack. Similarly, if one is being pushed then something probably was just popped. This is what I did to call viewDidDisappear and viewWillDisappear.
# pragma mark - UINavigationControllerDelegate Methods
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
static UIViewController *vcPointer = nil;
// If the previous view controller is still around then let's send a viewWillDisappear message
if (vcPointer != nil) {
if ([vcPointer respondsToSelector:#selector(viewWillDisappear:)]) {
[vcPointer viewWillDisappear:animated];
}
}
// Keep track of a pointer to the current viewController
vcPointer = viewController;
[viewController viewWillAppear:animated];
}
This code keeps a pointer reference to the last view controller that was pushed so that once we push another one we can pop the last one (if it still exists).
AFAIK, if you add a UINavigationController to a UIView via code, it won't send those messages to it's subviews by default. It will only do this if the UINavigationController received these calls itself. Maybe this is your problem (I don't know your view setup).
So, when adding the view of the UINavigationController, be sure to manually send it these messages.
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:rootViewController];
[navigationController viewWillAppear:NO];
[aView addSubview:navigationController.view];
[navigationController viewDidAppear:NO];
At least, this is what I found during development. Been searching for this for a long time and I still don't understand the rationale behind it.
You can always hide the default back navigation button and create your own with its own method to be called when pressed.
Execute whatever code you want there then pop the view.
I used this solution:
Add a custom button on the left side in the navigation bar
Let that button activate a custom method.
Disadvantage of this workaround: you will lose that nice arrow shaped "back" button. That can be solved as well with a custom image.
So here is my code.
Put this in your viewDidLoad:
// LeftButton in Navigation Bar
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonPushed:)];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftBarButton release];
Then add this method in the same .m file:
- (void) backButtonPushed: (id)sender {
// do what you want to do
}
dont forget in the .h file
- (void) backButtonPushed: (id)sender;
The viewWillDisappear & viewDidDisappear is called when a controller is popped or dismissed. The function is called on the fore-front view controller not on the UINavigationController itself. Did you possibly subclass and forget to call the super on something?

Dismissing UIPopoverController with -dismissPopoverAnimated: won't call delegate?

I have my UIPopoverController with self as a delegate:
I receive calls when I tap outside the popover controller, but when I tap inside I want to dismiss too, so I use -dismissPopoverAnimated: but delegate is not called in this case.
Is this normal? Is this a bug or I am doing something wrong?
newDocPopoverController = [[UIPopoverController alloc] initWithContentViewController:vc];
[newDocPopoverController setPopoverContentSize:CGSizeMake(240, 44*4)];
[newDocPopoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
[newDocPopoverController setDelegate:self];
UPDATE:
Oh, regardless the origin of the problem (Whether is a bug or this is the intended behavior) calling the delegate by myself solves the problem :)
When the contentViewController's view is touched I will call parent UIPopoverController's delegate a call.
if ([parentPopoverController.delegate popoverControllerShouldDismissPopover:parentPopoverController]){
[parentPopoverController dismissPopoverAnimated:YES];
[parentPopoverController.delegate popoverControllerDidDismissPopover:parentPopoverController];
}r];
That's normal, expected behavior.
Quoting the Apple docs on popoverControllerDidDismissPopover::
The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated: method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated: method.
Programmatically the popoverControllerDidDismissPopover does not get called and won't dismissed, you'll have to call the delegate yourself:
[self.PopUp dismissPopoverAnimated:YES];
[self.PopUp.delegate popoverControllerDidDismissPopover:self.PopUp];
Where PopUp is the parent UIPopoverController
Hope this helps
Cheers
Al
set the delegate first;
yourPopup.delegate=self;
then some where in your code (May be in Particular Method call due some event).
use the following code;
[self.yourPopUp dismissPopoverAnimated:YES];

remove ViewController from memory

I hope to load an ViewController and do something then unload (viewDidUnload) it from memory.
if (self.vViewController5.view.superview==nil)
{
ViewController5 *blueController =
[[ViewController5 alloc] initWithNibName:#"View5" bundle:nil];
self.vViewController5 = blueController;
[self.vViewController5 setDelegate:self];
[blueController release];
}
[self presentModalViewController:vViewController5 animated:YES];
later, call
[self dismissModalViewControllerAnimated:YES];
but I found that dismissModalViewControllerAnimated does not trigger the event viewDidUnload of Viewcontroller5.
I try function release but it caused program collapse.
I also try removeFromSuperView but it does not trigger the event ViewDidUnload neither.
Welcome any comment
Thanks
interdev
viewDidUnload is only called when your application receives a memory warning, and the view is not active. It's generally used for releasing retained views (including IBOutlets). You can use viewWillDisappear or viewDidDisappear to respond to a dismissal (dealloc will also be called eventually, but not necessarily exactly when the view is dismissed).
A dismissed UIViewController will not trigger the viewDidUnload, as you have discovered. However it will trigger viewWillDisappear method, if that suit your needs.