计算器中的分段控制

时间:2015-11-22 15:18:49

标签: ios iphone swift math uisegmentedcontrol

我试图制作一个包含两个值的分段控件,但是我在制作它时遇到了麻烦。我希望将输入乘以在分段视图中选择的任何一侧并显示在标签中,但我一直遇到错误。有帮助吗?

@IBOutlet var input: UITextField!
@IBOutlet var output: UILabel!
@IBOutlet var controller: UISegmentedControl!
@IBAction func convert(sender: UIButton) {
}






@IBAction func change(sender: AnyObject) {
    if controller.selectedSegmentIndex == 0 {
       result = number1 * 1
    }
    if controller.selectedSegmentIndex == 1 {
        result = number1 * 5
    }



    let number1 = Double(input.text!)
    label.text = "\(result)"

}

1 个答案:

答案 0 :(得分:3)

为什么在申报前使用“number1”? 试试这个:

@IBOutlet var input: UITextField!
@IBOutlet var output: UILabel!
var result:Double! //fixed "cannot be applied to Double and Int." problem
@IBOutlet var controller: UISegmentedControl!
@IBAction func convert(sender: UIButton) {
}


@IBAction func change(sender: AnyObject) {
let number1 = Double(input.text!)
    if controller.selectedSegmentIndex == 0 {
       self.result = number1 * 1
    }
    if controller.selectedSegmentIndex == 1 {
        self.result = number1 * 5
    }




    label.text = "\(result)"

}