0

我正在以编程方式创建一些UITextFields,当我尝试委派它们时,我收到以下警告:

NAVPlanViewController *const __strong' 到不兼容类型的参数id<UITextFieldDelegate>

textHeight = [[UITextField alloc] initWithFrame:CGRectMake(0, 120, 160, 40)];
[textHeight setDelegate:self];

.c我的课堂上,我添加了@interface NAVPlanViewController : UIViewController <UITextViewDelegate>. 另外,我需要限制文本字段只接受数字,我想用这段代码来做,但因为我UITextField无法Delegate检查它:

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

    if (!string.length) {
            return YES;
    }

    if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] 
                                         invertedSet]].location != NSNotFound){
        //do something;
        return NO;
    }
4

2 回答 2

2

在您的NAVPlanViewController接口定义中,<UITextViewDelegate>应该是<UITextFieldDelegate>.

于 2013-10-10T11:19:45.750 回答
1

@neilco 是正确的符合喜欢

@interface YourViewController ()<UITextFieldDelegate>
{

}
@end

并且除了数字,将keyboardType设置为UIKeyboardTypeNumberPad,如

UITextField *age = [UITextField autolayoutView];
age.placeholder = kPlaceholderToEnterAge;
age.borderStyle = UITextBorderStyleRoundedRect;
age.clearButtonMode = UITextFieldViewModeWhileEditing;
age.returnKeyType = UIReturnKeyDefault;
age.keyboardType = UIKeyboardTypeNumberPad;
age.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
age.textAlignment = NSTextAlignmentCenter;
于 2013-10-10T12:54:06.887 回答