2017-01-26 1 views
0

저는 매우 기본적인 앱을 만들려고합니다. 앱의 한 부분은 4 개의 textField와이 textField의 합을 계산하는 버튼이 있다는 것입니다. 내가 직면하고있는 문제는 첫 번째 textField에 값 10을 입력 한 다음 버튼을 누릅니다. 결과는 10이 될 것입니다. 그러나 다른 텍스트 필드에 아무 것도 입력하지 않고 다시 누르면 20이됩니다! 또한, 다른 textField 중 하나에 20을 입력하면 결과는 40입니다 !! 결과는 30이 아니어야합니다 40! 버튼을 누를 때 모든 textFields에 0을 할당한다고 생각한 가능한 옵션 (가능한 한 아직 시도하지 않음). 하지만 앱을 더 똑똑하게 만들고 결과를 추적 할 수 있기를 바랍니다.스위프트 3에서 이미 계산 된 UITextField 값을 무시하는 방법은 무엇입니까?

@IBAction func calBtnPressed(_ sender: UIButton) { 
     var benifit:[Double] = [] 

    var textFields: [Double] = [] 



    if initialBalance.text?.isEmpty ?? true { 

      // do nothing 

    } else { 

     if let temp = initialBalance.text { 

      // these lines of code will convert arabic numbers to English ones in case the user uses Arabic number 

      let initialStr: String = temp 
      let initialFormatter: NumberFormatter = NumberFormatter() 
      initialFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale! 
      let initialFinal = initialFormatter.number(from: initialStr) 
      benifit.append(Double(initialFinal!)) 
     } 
    } 



    if income.text?.isEmpty ?? true { 

     // do nothing 


    } else { 

     if let temp = income.text { 

     // these lines of code will convert Arabic numbers to English ones in case the user uses Arabic number 

      let incomeStr: String = temp 
      let incomeFormatter: NumberFormatter = NumberFormatter() 
      incomeFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale! 
      let incomeFinal = incomeFormatter.number(from: incomeStr) 
      benifit.append(Double(incomeFinal!)) 
     } 
    } 



    if salaries.text?.isEmpty ?? true { 

     // do nothing 


    } else { 

     if let temp = salaries.text { 

      let salariesStr: String = temp 
      let salariesFormatter: NumberFormatter = NumberFormatter() 
      salariesFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale! 
      let salariesFinal = salariesFormatter.number(from: salariesStr) 
      textFields.append(Double(salariesFinal!)) 
     } 
    } 


    if tools.text?.isEmpty ?? true { 

     // do nothing 

    } else { 

     if let temp = tools.text { 

      let toolsStr: String = temp 
      let toolsFormatter: NumberFormatter = NumberFormatter() 
      toolsFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale! 
      let toolsFinal = toolsFormatter.number(from: toolsStr) 
      textFields.append(Double(toolsFinal!)) 
     } 
    } 



    if maintinance.text?.isEmpty ?? true { 

     // do nothing 

    } else { 

     if let temp = maintinance.text { 

      let maintinanceStr: String = temp 
      let maintinanceFormatter: NumberFormatter = NumberFormatter() 
      maintinanceFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale! 
      let maintinanceFinal = maintinanceFormatter.number(from: maintinanceStr) 
      textFields.append(Double(maintinanceFinal!)) 
     } 
    } 


    if other.text?.isEmpty ?? true { 

     // do nothing 


    } else { 

     if let temp = other.text { 

     let otherStr: String = temp 
     let otherFormatter: NumberFormatter = NumberFormatter() 
     otherFormatter.locale = NSLocale(localeIdentifier: "EN") as Locale! 
     let otherFinal = otherFormatter.number(from: otherStr) 
     textFields.append(Double(otherFinal!)) 
     } 
    } 

    for textField in textFields { 

     sumExpenses += textField 
    } 

    for ben in benifit{ 

     sumBenifit += ben 
    } 

    totalExpenses.text = String(sumExpenses) 

    totalAfterSubtractingExpenses.text = String(sumBenifit - sumExpenses) 

    sumBenifit -= sumExpenses 

} 
+0

문제와 관련이 없지만이 코드에는 배열을 사용해야합니다. 서로 다른 버튼에 대해서만 동일한 다섯 줄을 여러 번 반복하면 코드를 읽기 어렵게하고 간결하게 만듭니다. –

+0

사실, 당신은 이미 textFields의 배열을 가지고 있습니다. 왜 그렇게 사용하지 않았습니까? –

+0

의견을 보내 주셔서 감사합니다.하지만 이해할 수 있는지 잘 모르겠습니다. 어느 5 줄을 반복할까요? 아라비아 숫자를 영어로 변환하는 것은 5 줄입니까? 나는 정말로 내 코드를 더 좋아하기를 정말 좋아한다. – user2066392

답변

2

내가 문제를 찾은 것 같아 : 도움이된다면

, 여기에 합계를 계산 버튼 내부의 코드입니다.

func에서 선언되지 않은 변수 sumBenefit을 사용하므로 UIViewController에 선언되어 있다고 가정합니다.

인스턴스 변수이므로 버튼을 클릭 할 때마다 재설정되지 않습니다. 나는 또한 당신이 원하는 가정을 만드는 중이라서

sumExpenses = 0 
for textField in textFields { 
    sumExpenses = Int(textField.text)! 
} 

sumBenefit = 0 
for ben in benefit { 
    sumBenefit += ben 
} 

: 당신이 sumExpensessumBenefits 버튼을 누를 때마다의 값을 재설정하려면

, 당신은 이런 식으로 뭔가를해야 할 것이다 첫 번째 for-loop의 textField에있는 숫자입니다. sumExpensesInt (또는 그 외 다른 숫자)이면 sumExpenses += textField이 컴파일되지 않기 때문입니다. 그 텍스트를 textField으로 가져와 Int으로 변환해야합니다.

다시 말씀 드리지만, 아직 명확하지 않은 내용이긴하지만, 더 이상 설명이 필요하지 않은 경우 나에게 알려주십시오.

+0

sumBenefit은 내가 제공 한 코드와 동일한 클래스 안에있는 전역 변수입니다. 나는 너의 제안을 시도하고 여기로 돌아갈거야 – user2066392

+0

나는 그것을 빨리 시험해 보았다. 고맙습니다 !!!! – user2066392

+0

@ user2066392 문제는 없습니다. 도와 줄 수있어서 기뻐 :) –

관련 문제