1
我正在使用cocos2d CClayer。textFiled shouldChangeCharactersInRange函数不起作用
我提交了TextField类以使用提交名称。
我跟着一些博主,设法显示textFiled,但shouldChangeCharactersInRange根本没有调用。
如何调用shouldChangeCharactersInRange? (我没有使用xib)
等待你的帮助。
代码波纹管。 (在Submit.h)
@property (nonatomic, retain) UITextField *mTextField;
@property (readonly) NSString *enteredText;
(在Submit.m)
-(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle{
if(self = [super initWithTitle:title message:message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)okButtonTitle, nil]) {
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 65.0, 260.0, 25.0)];
[theTextField setBackgroundColor:[UIColor whiteColor]];
[self addSubview:theTextField];
self.mTextField = theTextField;
[theTextField release];
}
return self;
}
- (BOOL)textField: (UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > MAXLENGTH) ? NO : YES;
}
(在GameLayer.h)
@property (nonatomic, strong) Submit *submitForm;
(在GameLayer.m)
-(void) submit:(id) sender {
Submit *prompt = [Submit alloc];
prompt = [prompt initWithTitle:@"Post Score" message:@"Enter Your Name\n\n\n" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Okay"];
CGAffineTransform moveDown = CGAffineTransformMakeTranslation(0,-10);
[prompt setTransform:moveDown];
self.submitForm = prompt;
[submitForm show];
[prompt release];
}