2016-11-18 132 views
1

我有必須調用檢查手機號碼範圍和其他格式的方法。但是這種方法不會被調用:(shouldChangeCharactersInRange沒有被調用。?

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 
    ProfileBasicCell *cell=[_tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
    if (cell.txtMobile.text== textField && string.length>0) { 
     NSString *newText=[textField.text stringByReplacingCharactersInRange:range withString:string]; 
     if (newText.length<=15) { 
      if (newText.length==4 || newText.length==8) { 
       textField.text=[newText stringByAppendingString:@"-"]; 
       return NO; 
      } 
     return YES; 
    } else { 
     return NO; 
    } 
    } 
    return YES; 
} 
+2

檢查您的文本字段是否包含委託集? – jokerday

+0

[cell.txtMobile setDelegate:<#(id _Nullable)#>]; 我應該在<<#(id _Nullable)#>中輸入什麼表達式 @deyanlai –

回答

2

難道u盤委託?

class yourViewController: UITextFieldDelegate { 

} 

self.yourTextField.delegate = self; 
0

你需要設置UITextFieldDelegate在控制器中像這樣

class yourViewController: UITextFieldDelegate { 

} 

,那麼你需要以這種方式給你的UITextfield代表

yourTextfield.delegate = self 
0

您需要設置委託。有兩種方法:

  1. 設置它通過分鏡

要設置通過故事板,右鍵點擊文本框,然後拖動delegateView Controller

  • 以編程方式設置它
  • UITextFieldDelegate添加到視圖控制器,併爲您的文本字段設置委託。這樣的事情:self.textField.delegate = self;

    +0

    我認爲文本字段沒有數據源。 – Adeel

    +0

    哦,是的,沒錯。我會編輯我的答案 – Munahil

    +0

    哦,是的,沒錯。我會編輯我的答案 – Munahil