UITextField shouldChangeCharactersInRange Delegate not working

风流意气都作罢 提交于 2019-12-12 01:05:43

问题


I am having trouble with a new view I have created, I have a registration view that has a single UITextField on it and a UIButton.

I call this view from another view like so

//otherview.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    RegistrationAlertViewController *regreg = [[RegistrationAlertViewController alloc] init];
    [self.view addSubview:regreg.view];

}

Then I create my regregview like this

//regregView.h

#import <UIKit/UIKit.h>

@interface RegistrationAlertViewController : UIViewController <UITextFieldDelegate> {


    // textfields for registration
    IBOutlet UITextField *registrationTextFieldA;


}

// textfields for registration
@property (strong, nonatomic) IBOutlet UITextField *registrationTextFieldA;

@end

//regregView.m

#import "RegistrationAlertViewController.h"

@interface RegistrationAlertViewController ()

@end

@implementation RegistrationAlertViewController

@synthesize registrationTextFieldA;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    registrationTextFieldA = [[UITextField alloc] init];
    registrationTextFieldA.delegate = self;

    [registrationTextFieldA becomeFirstResponder];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if([textField.text length] > 4)
    {
        //Get next TextField... A simple way to do this:
//        UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
//        [newTextField becomeFirstResponder];
        return NO;
        //remember to set the tags in order
    }
    return YES; //you probably want to review this...
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if((textField.text.length + string.length) > 4)
    {
        //Get next TextField... A simple way to do this:
//        UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
//        [newTextField becomeFirstResponder];
        //remember to set the tags in order
    }
    return YES; //you probably want to review this...
}


@end

I have the two delegates in my regregView.m

  • textFieldShouldBeginEditing
  • shouldChangeCharactersInRange

for some bizarre reason textFieldShouldBeginEditing is entered when the view first loads but then when I start entering characters into registrationTextFieldA shouldChangeCharactersInRange is never being entered for some bizarre reason.

any help figuring out why my delegates are not working properly would be greatly appreciated.


回答1:


Include UITextFielDelegate category in yourClass.h file and Try this code .

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    int length = textField.text.length - range.length + string.length;
    if(length > 4)
    {
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if([textField.text length] > 4)
    {
        return NO;
    }
    return YES; //you probably want to review this...
}

I hope it helps you.




回答2:


Don't allocate UITextField instance in viewDidLoad method. Replace you code with this :

- (void)viewDidLoad
{
    registrationTextFieldA.delegate = self;
    [registrationTextFieldA becomeFirstResponder];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

Hope it helps you.




回答3:


Add UItextField delegate from your xib outlet and declare delegate protocol <uitextfielddelegate> in your .h file.

Definitely it will work fine for You.

Good Luck !!!!




回答4:


A wild guess here. The problem is in your otherview.m

- (void)viewDidLoad 
{
     [super viewDidLoad];

     RegistrationAlertViewController *regreg = [[RegistrationAlertViewController alloc] init];
     [self.view addSubview:regreg.view];
}

Try making a strong property of RegistrationAlertViewController on top of your otherview.m

@property (nonatomic, strong) RegistrationAlertViewController *regreg;

Then in your view did load, you can do

- (void)viewDidLoad 
{
     [super viewDidLoad];

     RegistrationAlertViewController *regreg = self.regreg;
     if (regreg == nil)  
    {
        self.regreg = [[RegistrationAlertViewController alloc] init];
    }

     [self.view addSubview:regreg.view];

}

Hope that works.. Just seems like a similar problem I ran into before.. Good luck




回答5:


As you are initializing the view while implementing this file.So set delegate in init method not in viewDidLoad.

@synthesize registrationTextFieldA;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    registrationTextFieldA = [[UITextField alloc] init];
    registrationTextFieldA.delegate = self;
    [registrationTextFieldA becomeFirstResponder];

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


来源:https://stackoverflow.com/questions/17540096/uitextfield-shouldchangecharactersinrange-delegate-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!