【发布时间】:2013-07-09 04:45:06
【问题描述】:
我在创建新视图时遇到问题,我有一个注册视图,上面有一个 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
- 应该改变CharactersInRange
由于某些奇怪的原因 textFieldShouldBeginEditing 在视图首次加载时输入,但当我开始将字符输入 registrationTextFieldA shouldChangeCharactersInRange 时永远不会输入出于某种奇怪的原因。
任何帮助找出我的代表不能正常工作的原因将不胜感激。
【问题讨论】:
-
您已在属性中将文本字段声明为 IBOutlet,但您自己分配它。如果文本字段不是来自 XIB 文件,则删除 IBOutlet 属性。
-
你附上 UITextField 的委托了吗?
-
@HurkNburkS 试试看
-
好的我已经完全重新启动视图以摆脱任何可能破坏事物的代码..我仍然在编辑 UITextField 时收到这个 EXC BAD ACCESS ......它让我发疯我仍然正在寻找解决方案,我会告诉你我是如何上网的
-
原来,我不知道为什么,但是当我在 ViewController 中声明所有内容作为子视图添加到 mainView 时,delegatemethods 不起作用。但是,当我只是将所有内容添加到 mainView 时,委托方法就可以工作了......我不确定如何解决这个问题。有人有什么想法吗? :P
标签: iphone ios objective-c uitextfield uitextfielddelegate