0

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

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

//其他视图.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时,由于某些奇怪的原因永远不会输入。

任何帮助找出为什么我的代表不能正常工作的帮助将不胜感激。

4

5 回答 5

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

我希望它对你有帮助。

于 2013-07-09T05:26:03.440 回答
0

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

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

希望它可以帮助你。

于 2013-07-09T05:19:00.843 回答
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];

}

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

于 2013-07-09T06:51:52.880 回答
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.
}
于 2013-07-09T09:00:05.520 回答
0

从您的 xib 出口添加 UItextField 委托,并<uitextfielddelegate>在您的 .h 文件中声明委托协议。

绝对会为您工作。

祝你好运 !!!!

于 2013-07-09T08:43:59.030 回答