2013-10-10 48 views
0

I'm創造一些UITextField小號編程,當我試圖委派他們,我收到這樣的警告:代表文本框,並限制只能輸入數字

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; 
    } 
+2

請檢查您的代理要使用的UITextField替換 .... ....接口NAVPlanViewController:UIViewController Ravindhiran

回答

2

在你NAVPlanViewController接口定義,<UITextViewDelegate>應是<UITextFieldDelegate>

+0

是的,我精疲力竭,完全概述了這一點。謝謝! –

+0

您確定您正在設置代表嗎? – neilco

1

@neilco是正確的符合喜歡

@interface YourViewController()<UITextFieldDelegate> 
{ 

} 
@end 

並以惟獨數字,鍵盤類型設置爲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; 
+0

這不適用於ipad,因爲它沒有數字鍵盤,並且粘貼字符(不是數字)也是可能的。 –

相關問題