저는 방금 Swift로 시작했고 팁 계산기를 만들고 싶었습니다. 그러나 나는 몇 가지 문제에 직면했고 누군가가 나를 올바른 방향으로 안내 할 수 있기를 바랐습니다. 에서 getBillAmt
기능, 내 총은 만 표시, 표시 / 업데이트되지 billAmt
나는 갱신에 총을 얻을 aa는 몇 가지 다른 옵션을 시도했지만, 그것을 얻을 수 없습니다. 아래에서 나의 가장 최근 시도.
이전 시도에서 내 문제는 Int를 Float로 변환 할 수 없었습니다.
변수,
var tipPercentAmt = Int()
var billAmt = Float() //bill before tip
var total = Float()
사용자가 팁 양으로 슬라이드 할 수있는 슬라이더 (int로)
@IBAction func tipAmtVal(sender: UISlider) {
let tipPercentAmt = Int(sender.value)
}
그리고 사용자로부터 청구 금액을 텍스트 필드 (부 동체)에서 가져온 다음 여기에 팁을 추가하여 총액을 레이블에 표시합니다.
@IBAction func getBillAmt(sender: UITextField) {
let billAmt = Float(billAmtText.text!)
let total = billAmt! + (Float(tipPercentAmt / 100)
tipAmtLabel.text = "\(total)"
}
//cause labels to update when user hits return
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
실제로 getBillAmt
함수를 호출하도록 설정된 것은 없습니다 . 텍스트 필드가 델리게이트로 올바르게 설정되었다고 가정하면 textFieldShouldReturn
함수가 호출됩니다.
getBillAmount
거기에서 전화해야합니다 .
func getBillAmt(textField: UITextField) {
if let text = textField.text {
if let billAmt = Float(text) {
let total = billAmt + (Float(tipPercentAmt / 100)
tipAmtLabel.text = "\(total)"
}
}
}
//cause labels to update when user hits return
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
getBillAmt(textField: textField)
return false
}
보시다시피 다른 몇 가지 수정 사항도 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다