I have a weird problem where a UIPopovercontroller is immediately deallocating its content view controller after loading the popover, and then reinitializing it.
My goal is to read a textField when the popover is being dismissed.
My impression was that I create a UIViewController and set it as the content view controller for the popover. The PopoverViewController will then retain the content view controller and I can (auto)release it.
Later, when the popover is being dismissed, it will release the popover (and with it the content view controller). But that's not working. This is my relevant code:
- (IBAction)popoverButton:(id)sender {
// Create & Initialize content view controller
ContentViewController* cvc = [[[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil] autorelease];
// Create, initialize and load popover
popoverController = [[UIPopoverController alloc] initWithContentViewController:cvc];
[popoverController setDelegate:self];
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
NSLog(@"popoverButton: %@, retainCount: %d", cvc, [cvc retainCount]);
}
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)senderPopoverController
{
NSLog(@"popover should dismiss");
ContentViewController *dvc = (ContentViewController *)([popoverController contentViewController]);
NSLog(@"%@ %@ %@", dvc, [dvc testTextfield], [[[dvc testTextfield] text] description]);
return YES;
}
ContentViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[self testTextfield] setText:@"Bla"];
NSLog(@"viewDidLoad: %@", testTextfield);
}
- (void)dealloc {
NSLog(@"dealloc: %@", testTextfield);
[testTextfield release];
[super dealloc];
}
When I open the popover, the Log would be (I think the order of the output does not represent the order when it is actually called):
Popover Test[2363:707] viewDidLoad: <UITextField: 0x185750; ...>
Popover Test[2363:707] viewDidLoad: (null)
Popover Test[2363:707] popoverButton: <ContentViewController: 0x1844e0>, retainCount: 4
Popover Test[2363:707] dealloc: <UITextField: 0x185750; ...>
And when I dismiss it:
Popover Test[2363:707] popover should dismiss
Popover Test[2363:707] <ContentViewController: 0x1844e0> (null) (null)
Popover Test[2363:707] popover did dismiss
Popover Test[2363:707] <UIPopoverController: 0x184860>
Popover Test[2363:707] dealloc: (null)
So my questions would be:
- Why is the ContentViewController deallocated and initialized a second time?
- Why do the outlets (textField) not work anymore when its loaded the second time?
If I could solve this, I would be able to read from the textField in popoverControllershouldDismissPopover
I just wanted to use retainCount for tracing, it is not reliable? – michaelk Jan 31 '12 at 19:44