我在创建新视图时遇到麻烦,我在注册视图上有一个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,但是随后当我开始向registrationText中输入字符时,出于某些奇怪的原因,永远不会输入shouldChangeCharactersInRange。
非常感谢您弄清为什么我的代表不能正常工作的任何帮助。
最佳答案
在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...
}
希望对您有帮助。
关于iphone - UITextField shouldChangeCharactersInRange委托(delegate)不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17540096/