textField shouldChangeCharactersInRange函数不起作用

时间:2012-02-10 02:41:05

标签: textfield cclayer

我正在使用cocos2d CClayer。

我提交了TextField Class以使用提交名称。

我跟踪了一些博客并设法显示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];   
}

1 个答案:

答案 0 :(得分:1)

如果您尝试调用UITextField Delegate方法

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:  (NSRange)range replacementString:(NSString *)string

你必须设置uitextfield的委托属性。

As,

theTextField.delegate = self;