michaelk
michaelk

Reputation: 2127

Popover deallocating content view controller directly after initialization

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:

  1. Why is the ContentViewController deallocated and initialized a second time?
  2. 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

Upvotes: 0

Views: 1767

Answers (1)

bbum
bbum

Reputation: 162712

Since ContentViewController is your class, implement the appropriate init* method (if you haven't already), set a break point and the debugger will stop on it at each allocation, answering your question as to why it is being recreated.

Note that retainCount is useless; don't call it.


Ah -- OK -- so, you are creating one instance when you are loading the nib file and a second instance directly in your code. Instead, you want an outlet somewhere that is connected to the instance in the nib file.

As for retainCount; Calling -retainCount Considered Harmful and When to use -retainCount?

Upvotes: 1

Related Questions