I am programatically creating a UITextField
, adding it to view and then start editing by calling
[textField becomeFirstResponder];
But the issue is this call triggers call to textFieldDidEndEditing: delegate method. The stack trace points to [textField _resignFirstResponder]
which is invoked by becomeFirstResponder. This is happening on simulator and iOS 7
. How do I avoid this ? This is causing lot of issues as I don't want textFieldDidEndEditing:
to be called without keyboard getting dismissed.
EDIT : Here is how I create UITextField
.
UITextField *titleField = [[[UITextField alloc] init] autorelease];
[titleField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
titleField.textColor = [UIColor colorWithWhite:1.f alpha:0.8];
titleField.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:40.f];
titleField.textAlignment = NSTextAlignmentCenter;
titleField.borderStyle = UITextBorderStyleBezel;
titleField.autocorrectionType = UITextAutocorrectionTypeNo;
// titleField.borderStyle = UITextBorderStyleLine;
titleField.frame = CGRectMake(0, 0, 200, 80);
titleField.center = self.previewView.center;
titleField.delegate = self;
NSDictionary *titleDictionary = [self titleDictionary];
if (titleDictionary) {
titleField.text = [titleDictionary objectForKey:kTitleStringKey];
NSString *fontName = [titleDictionary objectForKey:kTitleFontNameKey];
CGFloat fontSize = [[titleDictionary objectForKey:kTitleFontSizeKey] floatValue];
titleField.font = [UIFont fontWithName:fontName size:fontSize];
NSData *textColorData = (NSData *)[titleDictionary objectForKey:kTitleTextColorKey];
titleField.textColor = [NSKeyedUnarchiver unarchiveObjectWithData:textColorData];
CGSize size = [titleField.text sizeWithFont:titleField.font];
titleField.frame = CGRectMake(0, 0, size.width + 40, 80);
titleField.center = self.previewView.center;
}
[self.view addSubview:titleField];
[titleField becomeFirstResponder];
Ok I found it. I had a UILongPressGestureRecognizer that triggered adding UITextField routine, and that is incidentally called twice on long press (need to figure out why, but its a separate issue). So becomeFirstResponder was called twice.