1

我正在使用 cocos2d CClayer。

我制作了提交 TextField 类以使用提交名称。

我关注了一些博主并设法显示 textFiled,但 shouldChangeCharactersInRange 根本没有调用。

我如何调用 shouldChangeCharactersInRange?(我没有使用xib)

等待您的帮助。

代码如下。(在提交.h)

@property (nonatomic, retain) UITextField *mTextField;
@property (readonly) NSString *enteredText;

(在提交.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];   
}
4

1 回答 1

1

如果您尝试调用 UITextField 委托方法

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

,

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

作为,

theTextField.delegate = self;
于 2012-02-21T07:16:19.200 回答