2017-03-13 1 views
0

다음은 숫자 서식을 지정하고 텍스트 필드에 다시 할당하는 코드입니다. 몇 가지 문제가있는 텍스트를 삭제하는 동안 문자 대신 문자 전체가 삭제되며 텍스트를 다시 입력 할 수 없습니다. 이 문제를 해결하도록 안내해주세요.numberformatter (스타일 통화) 삭제가 작동하지 않습니다.

let currencyFormatter = NumberFormatter() 
let decimalFormatter = NumberFormatter() 
var currentString = "" 

func formatCurrency(_ string: String) { 
    let numberFromField = (NSString(string: string).doubleValue)/100 
    activeField?.text = currencyFormatter.string(from: NSNumber(value: numberFromField)) 
} 

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 
    if finalTextCount >= maxCharactersLimit && string != ""{ 
     return false 
    } 

    switch string { 
    case "0","1","2","3","4","5","6","7","8","9": 
     currentString += string 
     if textField == kmMinTextField || textField == kmMaxTextField { 
      formatKms(currentString) 
     } else { 
      formatCurrency(currentString) 
     } 
    default: 
     if string.characters.count == 0 && currentString.characters.count != 0 { 
      currentString = String(currentString.characters.dropLast()) 
      if textField == kmMinTextField || textField == kmMaxTextField { 
       formatKms(currentString) 
      } else { 
       formatCurrency(currentString) 
      } 
     } 
    } 

    return false 
} 

답변

0

코드를 실행했지만 이러한 문제가 없었습니다. (

var maxCharactersLimit = 10 

및 TextField를 : 당신이 XIB 또는 스토리 보드, '편집이 시작되면 지우기'또한 '

을 선택하지 않은 것을 다시 확인을 사용하는 경우, 나는이 작품을 만들기 위해 변수를 설정하는 데 필요한 _에 textField : UITextField에, shouldChangeCharactersIn 범위 : NSRange, replacementString 문자열 : 나는 텍스트 필드에서 텍스트를 삭제하면 문자열)

let finalTextCount = textField.text?.characters.count != nil ? textField.text!.characters.count : 0 
+0

"지우기 편집 시작"나는 문제에 직면하지만 확인되지 않습니다. 문자를 삭제하기 전에 통화 기호와 구분 기호를 제거해야합니까? – Dee

+0

나는 이것을 할 필요가 없었다. 통화 포맷터에 numberStyle = .currency 및 currencySymbol = "$"을 추가했으며 문자를 삭제 한 후 텍스트 필드는 $ 0.00입니다. 귀하가 설명하는 문제는 귀하가 게시 한 코드, 어딘가에있는 것처럼 보이지 않습니다. – farawaystars

+0

예. 둘 이상의 텍스트 필드를 유지하면 삭제에 문제가있는 것입니다. 시간 내 주셔서 감사합니다. 문제를 디버그하고 수정해야합니다. – Dee

관련 문제