0

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

michaelk
  • 2,107
  • 3
  • 15
  • 18

1 Answers1

1

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?

Community
  • 1
  • 1
bbum
  • 162,346
  • 23
  • 271
  • 359
  • Thanks for your reply. I have implemented a trace for all the init-methods, actually it is just initialized once. initWithNibName calls initWithCoder. Thats it, and then it is being deallocated, which I still can't figure out why.
    I just wanted to use retainCount for tracing, it is not reliable?
    – michaelk Jan 31 '12 at 19:44
  • I am not sure if I understand your answer correctly. My ContentViewController is stored in a separate nib file. The first time this is being loaded is when I call initWithNibFile: or am I wrong? When would be the second time then? – michaelk Jan 31 '12 at 23:04
  • Sorry -- I think I may have misread your comment. Are you sure it is being initialized twice? – bbum Feb 01 '12 at 06:55
  • I am not totally sure, all I have is the log I posted above, which looks like its being deallocated and then initialized again. But then, when looking at the initializers, they just seem to be called once. But, which instance is being deallocated the second time in the log above then? In Java I would debug the whole thing including the SDK to see whats going on, but I cannot do that with iOS :-/ All I want to do is load a very simple popover from a nib file, show it, and when its being dismissed, read the textField in it. Thanks very much for your help! – michaelk Feb 01 '12 at 19:11