UITextField shouldChangeCharactersInRange委托不起作用 [英] UITextField shouldChangeCharactersInRange Delegate not working

查看:87
本文介绍了UITextField shouldChangeCharactersInRange委托不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建新视图时遇到麻烦,我在注册视图上只有一个UITextField和一个UIButton.

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];

}

然后我像这样创建我的regregview

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

我的regregView.m中有两个代表.

I have the two delegates in my regregView.m

  • textFieldShouldBeginEditing
  • shouldChangeCharactersInRange

出于某种奇怪的原因,在视图第一次加载时输入了 textFieldShouldBeginEditing ,但随后当我开始在 registrationTextFieldA 中输入字符时,从未输入 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.

推荐答案

在yourClass.h文件中包含UITextFielDelegate类别,然后尝试使用此代码.

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...
}

希望对您有帮助.

这篇关于UITextField shouldChangeCharactersInRange委托不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆