title: NSNotifications and removeObserver in a UIView
body: <p>I create a subclassed <code>UIView</code>:</p>
  
  <pre><code>BeneficialsFormView *view = [[BeneficialsFormView alloc] init];
  view.alpha = 0;
  view.delegate = self;
  
  view.screenView = [self.delegate displayEntityForm:view];
  </code></pre>
  
  <p>The delegate call is to display the <code>UIView</code> and returns a UIView that I use to disable background touches (just a UIView with black blackground color and reduced alpha).</p>
  
  <p>In the subclassed <code>UIView</code>, I add in keyboard notifications:</p>
  
  <pre><code>-(void)awakeFromNib {
      DLog(@"fired");
  
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardEvent:) name:UIKeyboardWillShowNotification object:nil];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardEvent:) name:UIKeyboardWillHideNotification object:nil];
  
      // UI Code
  }
  </code></pre>
  
  <p>I try to remove the <code>NSNotifications</code> in <code>dealloc</code>:</p>
  
  <pre><code>-(void)dealloc {
      DLog(@"fired");
  
      [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:UIKeyboardWillShowNotification];
      [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:UIKeyboardWillHideNotification];
  }
  </code></pre>
  
  <p>The issue is that <code>dealloc</code> is fired right after <code>awakeFromNib</code>:</p>
  
  <pre><code>2013-06-12 15:37:39.260  DEBUG | -[BeneficialsFormView init] | fired
  2013-06-12 15:37:39.287  DEBUG | -[BeneficialsFormView awakeFromNib] | fired
  2013-06-12 15:37:39.339  DEBUG | -[BeneficialsFormView dealloc] | fired
  </code></pre>
  
  <p>They all are fired withing .1 seconds of each other.  The subclassed <code>UIView</code> behaves exactly as I want it to; It shows, all its contents work and I can <code>removeFromSuperview</code> w/out issues.</p>
  
  <p>I was under the assumption <code>dealloc</code> would fire when all references to the subclassed <code>UIView</code> reached 0?</p>
  
  <p>I am using ARC.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>EDIT:</p>
  
  <p>Found out what I'm doing wrong:</p>
  
  <pre><code>-(id)init {
      DLog(@"fired");
      DLog(@"self: %@",self);
  
      self = [super init];
  
      if (self) {
          self = [[[NSBundle mainBundle] loadNibNamed:@"BeneficialsFormView" owner:self options:nil] objectAtIndex:0];
      }
  
      return self;
  }
  </code></pre>
  
  <p>So I create my view with my <code>init</code> method, which loads the xib file. <code>awakeFromNib</code> fires and then the object I create with my <code>init</code> file looses reference and is deallocated while the objected created from <code>awakeFromNib</code> is displayed.</p>
  
  <p>How can I fix this? Or am I doing something fundamentally wrong and creating a tear in the space-time continuum?</p>
  
  <pre><code>2013-06-12 18:15:33.957  DEBUG | -[BeneficialsFormView init] | fired
  2013-06-12 18:15:33.959  DEBUG | -[BeneficialsFormView init] | self: &lt;BeneficialsFormView: 0x1f0b1d50; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; layer = (null)&gt;
  
  2013-06-12 18:15:33.988  DEBUG | -[BeneficialsFormView awakeFromNib] | fired
  2013-06-12 18:15:33.989  DEBUG | -[BeneficialsFormView awakeFromNib] | self: &lt;BeneficialsFormView: 0x1e58da60; frame = (0 0; 350 400); autoresize = RM+BM; layer = &lt;CALayer: 0x1e5c60f0&gt;&gt;
  
  2013-06-12 18:15:34.044  DEBUG | -[BeneficialsFormView dealloc] | fired
  2013-06-12 18:15:34.045  DEBUG | -[BeneficialsFormView dealloc] | self: &lt;BeneficialsFormView: 0x1f0b1d50; frame = (0 0; 0 0); layer = &lt;CALayer: 0x1f0d4830&gt;&gt;
  </code></pre>