UITextField shouldChangeCharactersInRange委托不起作用

时间:2013-07-09 04:45:07

标签: iphone ios objective-c uitextfield uitextfielddelegate

我在使用我创建的新视图时遇到问题,我有一个注册视图,其上有一个UITextField和一个UIButton。

我从另一个视图中调用此视图

// otherview.m

- (void)viewDidLoad
{
    [super viewDidLoad];

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

}

然后我像这样创建我的regregview

// 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

中有两位代表
  • textFieldShouldBeginEditing
  • shouldChangeCharactersInRange

出于某些奇怪的原因 textFieldShouldBeginEditing 在视图首次加载时输入,但是当我开始将字符输入 registrationTextFieldA 时,永远不会输入shouldChangeCharactersInRange 出于某种奇怪的原因。

任何帮助,弄清楚为什么我的代表不能正常工作将不胜感激。

5 个答案:

答案 0 :(得分:1)

在yourClass.h文件中包含UITextFielDelegate类别并尝试此代码。

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

我希望它可以帮到你。

答案 1 :(得分:0)

不要在UITextField方法中分配viewDidLoad个实例。用以下代码替换你的代码:

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

希望它对你有所帮助。

答案 2 :(得分:0)

在这里疯狂猜测。 问题出在你的otherview.m

- (void)viewDidLoad 
{
     [super viewDidLoad];

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

尝试在otherview.m

之上创建RegistrationAlertViewController的强大属性
@property (nonatomic, strong) RegistrationAlertViewController *regreg;

然后在你的视图中加载,你可以做到

- (void)viewDidLoad 
{
     [super viewDidLoad];

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

     [self.view addSubview:regreg.view];

}

希望有效..看起来像我之前遇到的类似问题..祝你好运

答案 3 :(得分:0)

从xib outlet添加UItextField委托,并在.h文件中声明委托协议<uitextfielddelegate>

肯定会对你有用。

祝你好运!!!!

答案 4 :(得分:0)

在实现此文件时初始化视图。在init方法中设置委托不在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.
}