2016-09-23 5 views
0

사용자가 두 개의 개별 텍스트 필드에 숫자를 입력 할 수있는 응용 프로그램에서 장면을 만들려고합니다. 몇 가지 기본 수학을 수행하고 클릭 할 때 레이블에 출력을 침범합니다 "계산 버튼". 나는 비디오를 보았고, 코드를 변경했으며, 내가 원하는대로 작동하지 못하는 것 같았고, 내 앱은 계속 충돌했다. 지금까지 가지고있는 간단한 코드가 있습니다. 누구나 내가 잘못 들어 있고 뭘 바꿀 필요가 있는지 설명 할 수 있습니까? 감사합니다. 고마워요!간단한 텍스트 필드 변경할 레이블 필드

import UIKit 

class DopamineCalculator: UIViewController { 

//Patient weight Input 

@IBOutlet weak var patientWeight: UITextField! 


//calculate button 
@IBAction func calculateButton(sender: AnyObject) { 

    let weightPatient = Double(self.patientWeight.text!) 

    dripAnswer.text = String(weightPatient * 2) 

} 


//Clear button action 
@IBAction func clearButton(sender: AnyObject) { 
    self.patientWeight.text=nil 
    //self.dosageDesired.text=nil 
    self.dripAnswer.text=nil 
} 


//Drip Rate Answer Label 
@IBOutlet weak var dripAnswer: UILabel! 

"삭제"버튼이 작동하지만 "계산"버튼이 작동하지 않습니다. 지금은 간단하게 유지하려고하는데 두 상자를 추가하면 작동하도록 할 수는 없습니다. 잘 작동

+0

은 무엇 충돌을 수행이 확장을 사용하여 도망? – Paulw11

+0

긴 목록이지만 이유는 기본적으로 내 "계산 버튼"버튼을 골라 둡니다. – beans217

+0

그건 이유가 아닙니다. 특정 행에 대한 특정 예외가 있습니다. – Paulw11

답변

0
var weightpatient = Double(self.patientweight.text!) 
    weightpatient = Double(weightpatient!*2) 
    let basicStr:String = String(format:"%f", weightpatient!) 
    dripanswer.text = String(basicStr) 

.. 여기

https://www.dropbox.com/s/p67f88qnnsekivn/Blur.zip?dl=0 시도하는 데모 링크가 나를

+0

그것은 나에게 아직도 웬일인지 충돌하고 있습니다. IBaction에서 입력 한 내용을 정확하게 복사했습니다. 내가 뭔가 잘못했는지 확인해 봤어? – beans217

+0

대답을 업데이트했습니다. – gurmandeep

+0

"2016-09-22 23 : 14 : 05.082 FCEMS Protocols [1193 : 351411] *** 캐치되지 않은 예외 'NSUnknownKeyException'로 인해 응용 프로그램을 종료 함, 이유 : '[ setValue : forUndefinedKey : ] :이 클래스는 키 계산 단추에 대해 키 값 코딩을 준수하지 않습니다 – beans217

0

을 알려 어쩌면 "!" weightpatient 후. 하위 코드는 내 시뮬레이터에서 잘 작동합니다 (xcode 7.3.1).

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var patientweight: UITextField! 

    @IBAction func calculatebutton(sender: AnyObject) { 
    let weightpatient = Double(self.patientweight.text!) 

    dripanswer.text = String(weightpatient! * 2) 

    } 

    @IBAction func clearbutton(sender: AnyObject) { 
    self.patientweight.text=nil 
    //self.dosagedesired.text=nil 
    self.dripanswer.text=nil 
    } 

    @IBOutlet weak var dripanswer: UILabel! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    } 
} 
0

먼저 환자의 체중이 숫자인지 확인해야합니다. 그런 다음

extension String { 
var isNumber: Bool 
{ 
    let range = self.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) 
    return (range == nil) 
} 
} 

당신이 문자열이 비어 있지 않은지 확인하고이 두 조건은 계산을 계속 충족 될 경우이 숫자를 포함 ...

@IBAction func calculateButton(sender: AnyObject) { 
    if patientWeight.text != "" && patientWeight.isNumber==true{ 
    let weightPatient = Double(self.patientWeight.text!) 
    self.dripAnswer.text = String(weightPatient! * 2) 
    } 
    else{ 
    self.dripAnswer.text = "Please enter weight" 
    } 
}