title: custom uitextfield with custom inputAccessoryView
body: <p>is strange to se no question about this problem in all stack overflow, but i can't find it :(</p>
  
  <p>So let me explain. I have a simple <code>UITextField</code> that should present a custom <code>inputAccessoryView</code>. </p>
  
  <p>The expected behavior is:</p>
  
  <ol>
  <li>tap in the textfield (ACTextField)</li>
  <li>show keyboard with a textfield and a button (with cancel title)</li>
  <li>type in some text (it have to appear in the <code>UITextField</code> named
  <code>inputField</code> )</li>
  <li>press enter and see the text appearing in the main textfield</li>
  </ol>
  
  <p>in case i press cancel the keyboard should disappear and let the main textfield unchenged</p>
  
  <p>pretty much like the Message App from Apple</p>
  
  <p>And all works well exept 2 things:</p>
  
  <ol>
  <li>if i spam key tapping on the keyboard (just to enter random text ;)
  ) the app freeze and Xcode show me CPU at 99% (no recovery possible)</li>
  <li>sometimes the memory occupation grow from 2/3 MB to 40 MB just after
  pressing enter and the app freeze again</li>
  </ol>
  
  <p>Let's show some code
  the .h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface ACTextField : UITextField
  
  @end
  </code></pre>
  
  <p>the .m</p>
  
  <pre><code>#import "ACTextField.h"
  
  @interface ACTextField () &lt;UITextFieldDelegate&gt;
  
  @property (nonatomic, retain) UITextField *inputField;
  @property (nonatomic, assign) BOOL keyboardOpen; // to check if keyboard is open
  @property (nonatomic, retain) NSTimer *timer; //added after an answer
  
  @end
  
  @implementation ACTextField
  
  // method called when timer fire
  - (void) fireTimer:(NSTimer *) timer
  {
      [self.timer invalidate];
      self.timer = nil; // this line thoesen't change anyting so can be deleted
      [self.inputField becomeFirstResponder];
  }
  
  - (id)initWithFrame:(CGRect)frame
  {
      self = [super initWithFrame:frame];
      if (self) {
          // Initialization code
          [self awakeFromNib];
      }
      return self;
  }
  
  -(void)awakeFromNib
  {
      self.delegate = self;
      self.keyboardOpen = NO;
  }
  
  - (void)cancelPressed:(id)sender
  {
      [self.inputField resignFirstResponder];
      [self resignFirstResponder];
  
      self.keyboardOpen = NO;
  }
  
  - (void)dealloc
  {
      NSLog(@"dealloc ACTextField %@", self);
      self.inputField = nil;
      self.timer = nil;
  }
  
  - (void) designAccessoryView
  {
      if (self.inputAccessoryView == nil) {
  
          CGRect frame = [[UIScreen mainScreen] bounds];
  
          self.inputAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 44)];
  
          self.inputAccessoryView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  
          int buttonWidth = 65;
          int padding = 5;
  
          UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
          cancel.frame = CGRectMake(padding, 0, buttonWidth, 44);
          [cancel setTitle:@"cancel" forState:UIControlStateNormal];
          [cancel setTitle:@"cancel" forState:UIControlStateHighlighted];
          [cancel addTarget:self action:@selector(cancelPressed:) forControlEvents:UIControlEventTouchUpInside];
          [self.inputAccessoryView addSubview:cancel];
  
          self.inputField = [[UITextField alloc] initWithFrame:CGRectMake(buttonWidth + 2*padding, 2, frame.size.width - buttonWidth - 3*padding, 40)];
          self.inputField.borderStyle = UITextBorderStyleRoundedRect;
  
          self.inputField.delegate = self;
  
          [self.inputAccessoryView addSubview:self.inputField];
          self.inputAccessoryView.backgroundColor = [UIColor lightGrayColor];
      }
  }
  
  - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
      return YES;
  }
  - (void)textFieldDidBeginEditing:(UITextField *)textField
  {
      if (!self.keyboardOpen &amp;&amp; textField == self)
      {
          self.inputField.text = self.text;
          [self designAccessoryView];
  
          self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                           target:self // modified to use the instance method fireTimer
                                         selector:@selector(fireTimer:)
                                         userInfo:nil
                                          repeats:NO];
  
          self.keyboardOpen = YES;
      }
  }
  - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
  {
      return YES;
  }
  
  - (void)textFieldDidEndEditing:(UITextField *)textField
  {
  }
  
  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  {
      return YES;
  }
  
  - (BOOL)textFieldShouldClear:(UITextField *)textField
  {
      return YES;
  }
  
  - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  
      self.text = self.inputField.text;
      [self cancelPressed:self];
  
      return YES;
  }
  
  @end
  </code></pre>
  
  <p>Any help will be greatly appreciated</p>
  
  <p>EDIT: I forgot to specify that i'm testing this on my iPhone 4s with IOS 7.0.4</p>
  
  <p>EDIT2: I tried using an <code>UITextField</code> Wrapper with pretty much the same code and all goes well. The behavior is the expected and the keyboard don't freeze anymore and the memory remain low as should be. Ok, but i need a <code>UITextField</code> subclass, not a wrapper :(</p>