我遇到了一個新的視圖,我創建了一個註冊視圖,它有一個UITextField和一個UIButton。UITextField shouldChangeCharactersInRange代表不工作
我把這種觀點從另一種觀點認爲,像這樣
//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是當視圖fi時輸入第一次加載,但然後當我開始輸入字符到registrationTextFieldAshouldChangeCharactersInRange永遠不會進入一些奇怪的原因。
任何幫助搞清楚爲什麼我的代表不能正常工作將不勝感激。
您已將textfield聲明爲屬性中的IBOutlet,但是您正在自行分配它。如果textfield不是來自XIB文件,則刪除IBOutlet屬性。 – Amit
你是否附加了UITextField的委託? – chandan
@HurkNburkS試試看行不行 – Warewolf