0

Got one here which is causing 'concern'...

I have a modal view controller which uses the keyboard. I am using a system to dismiss the keyboard from code on stackoverflow which appear to be the 'new way' of recognising taps outside the keyboard to dismiss it...

// Register for keyboard dismissal
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:nil usingBlock:^(NSNotification *notification) 
{
     UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)]autorelease];
     tap.numberOfTapsRequired = 1;
     tap.numberOfTouchesRequired = 1;
     tap.delegate = self;
     [self.view addGestureRecognizer:tap];
}];

[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:nil usingBlock:^(NSNotification *notification) 
{
    [self.view removeGestureRecognizer:[self.view.gestureRecognizers lastObject]];
}];

This works well but I notice that dealloc is not being called on my view conroller when I dismiss it via a Done button.

It seems to be something to do with the code above. If I comment out the executable code within the notification definition braces, then dealloc is called ok when the controller is dismissed....(keyboard has not been shown or dismissed, so code within braces has not executed)

Has anyone got any suggestions as to why dealloc is not being called when above code is implemented?

Thanks

Fitto

Fittoburst
  • 2,215
  • 2
  • 21
  • 33
  • Perhaps this gives some insight: http://stackoverflow.com/questions/4352561/retain-cycle-on-self-with-blocks – Rengers May 12 '12 at 15:56

1 Answers1

1

While using block it will retain "self", so you need to remove observer to dealloc.

rishi
  • 11,779
  • 4
  • 40
  • 59
  • Thanks but don't quite understand 'remove observer to dealloc' ????? The dealloc is not being called. If it were, then I can remove the observers, which I have but as I said, dealloc not called. Suggestions? – Fittoburst May 12 '12 at 09:40
  • OK. Figured it out thanks to your 'pointer'. Basically I store the id of the returned observer and then free it when required.... [[NSNotificationCenter defaultCenter]myObserver]; – Fittoburst May 15 '12 at 13:24