开发者

textFieldShouldBeginEditing not being called

开发者 https://www.devze.com 2023-04-12 08:41 出处:网络
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, textFi

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


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.


Have you set the UITextField delegate to "self"?


For anyone that comes here and didn't find a solution. My problem was that I created the textField in IB and then alloc one in my viewDidLoad. When I removed the instantiation, the delegate worked correctly as it was tied to the correct TF.

//I REMOVED these two lines because I created the textfield in IB
_nameTextField = [[UITextField alloc] init];
_priceTextField = [[UITextField alloc] init];


[_nameTextField setDelegate:self];
[_priceTextField setDelegate:self];


I also ran into the issue of not having textFieldShouldEndEditing or textFieldShouldReturn called. This occurred after updating my app's Storyboard.

The delegate methods were getting called when the UITextFields where part of the ViewController subclass.

But, when they were moved into a Scroll View within the ViewController, the methods were no longer called.

Setting their app delegates to self in ViewDidLoad fixed this problem.

self.emailField.delegate = self;
self.passwordField.delegate = self;


In Swift-3 following method required "_" before textField so that this delegate method will call. In my case it helps me.

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
}


Dont know if is a typo error on copying the code here but the method name is incorrect:

CORRECT - textFieldShouldBeginEditing

YOURS - textFieldShoulBeginEditing (missing "d")

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号