Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textFieldShouldBeginEditing not being called

I am trying to use textFieldShouldBeginEditing to disable the keyboard from showing up for a custom UITextField. I'm implementing all the UITextFieldDelegate methods. However, for some reason, textFieldShouldBeginEditing actually never gets called.

The following delegate methods ALWAYS get called:

– textFieldDidBeginEditing:
– textFieldShouldEndEditing:
– textFieldDidEndEditing:

The view is structured in the following way:

UIViewController which holds a scrollview. Depending on the state of the view, this ScrollView will contain a UIView with a list of custom UITextFields.

I'm running iOS 4.3.5 (8L1) on this device.

Any ideas?

Edit; added some code snippets:

UIViewController has the following interface

@interface AViewController: UIViewController<UITextFieldDelegate>

Once the UIViewController loads, I connect all UITextFields to the view using

aSubView.aTextField.delegate = self;

(Simplified) delegate implementations located in AViewController

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}

Custom UITextField code

Simplified implementation file --

#import "PVEntryTextField.h"
#import "EntryViewController.h"

@implementation PVEntryTextField
@synthesize isPasswordField, state, singleTap;

- (id)initWithCoder:(NSCoder *)inCoder
{
    if (self = [super initWithCoder:inCoder])
    {
         self.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
         self.textColor = [UIColor colorWithRed:51.0/255.0 
                                         green:51.0/255.0 
                                          blue:51.0/255.0 
                                         alpha:1.0];
         self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    }

    return self;
}

- (CGRect)textRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.origin.x + 16, bounds.origin.y,
                      bounds.size.width - 16*2 - 10, bounds.size.height);
}

- (CGRect) editingRectForBounds:(CGRect)bounds
{
    return [self textRectForBounds:bounds];
}

- (BOOL) canBecomeFirstResponder
{
    return YES;
}

- (void) updateState:(int) newState
{
     state = newState;
 }

- (void)dealloc
{
    [super dealloc];
}

 @end
like image 471
Charles Avatar asked Oct 12 '11 16:10

Charles


1 Answers

Is it posible that textFieldShouldBeginEditing is called by the default implementation of the method canBecomeFirstResponder?

Try implementing the method by [super canBecomeFirstResponder] or just removing it.

like image 127
lluismontero Avatar answered Sep 20 '22 16:09

lluismontero