该程序假设改变F到C并反转.使用Switch,它从on变为off和on,假设是C到f,off是F到c,并在文本字段中输入#underneath.
单击提交按钮时,它会在文本字段中显示什么,将其传输到预制算法中,然后将其显示在文本字段中.
单击提交按钮时,它会在文本字段中显示什么,将其传输到预制算法中,然后将其显示在文本字段中.
我相信转换正确但不会显示实际结果.或者它被转换的方式是错误的.
@IBOutlet weak var buttonClicked: UIButton! @IBOutlet weak var mySwitch: UISwitch! @IBOutlet weak var myTextField: UITextField! @IBOutlet weak var User: UITextField! func stateChanged(switchState: UISwitch) { if switchState.on { myTextField.text = "Convert to Celius" } else { myTextField.text = "Convert to Farheniet" } } @IBAction func buttonClicked(sender: UIButton) { if mySwitch.on { var a:Double? = Double(User.text!) a = a! * 9.5 + 32 User.text=String(a) mySwitch.seton(false,animated:true) } else { var a:Double? = Double(User.text!) a = a! * 9.5 + 32 User.text=String(a) mySwitch.seton(true,animated:true) } }
解决方法
我使用的是旧版本的XCode(6.4),因此我的代码与您的代码略有不同.根据我的理解你的函数buttonClicked应该采用AnyObject的UIButton的参数.您也不要在代码中调用函数stateChanged.以下代码应该有助于实现您的目标.
@IBOutlet weak var mySwitch: UISwitch! @IBOutlet weak var myTextField: UITextField! @IBOutlet weak var User: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. // sets the textfield to the intended conversion on load. if mySwitch.on { myTextField.text = "Convert to Celius" } else { myTextField.text = "Convert to Farheniet" } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // dispose of any resources that can be recreated. } // changes the myTextFiled text to the intended conversion when the switch is manually switched on or off @IBAction func switched(sender: AnyObject) { if mySwitch.on { myTextField.text = "Convert to Celsius" } else { myTextField.text = "Convert to Fahrenheit" } } // changes the myTextField text to intended reverse conversion after the buttonClicked func is completed. func stateChanged(switchState: UISwitch) { if switchState.on { myTextField.text = "Convert to Celsius" } else { myTextField.text = "Convert to Fahrenheit" } } // do the intended conversion(old version of XCode 6.4) @IBAction func buttonClicked(sender: AnyObject) { if mySwitch.on { var a = (User.text! as Nsstring).doubleValue a = (a-32)*(5/9) User.text="\(a)" mySwitch.seton(false,animated:true) stateChanged(mySwitch) } else { var a = (User.text! as Nsstring).doubleValue a = a * (9/5) + 32 User.text="\(a)" mySwitch.seton(true,animated:true) stateChanged(mySwitch) } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。