我有一个segmentedControl,它为字段提供字符串。我还有另外两个文本域。一个用于用户输入,第二个用于从segmentedcontrol文本字段和用户输入文本字段进行计算。当segmentedcontrolindex发生变化时,如何自动更新计算文本字段。 对于用户输入文本字段,textfielddidchange方法有效。谢谢
@IBOutlet weak var segmentedcontrol: UISegmentedControl!
@IBOutlet weak var textf: UITextField!
@IBOutlet weak var textf1: UITextField!
@IBOutlet weak var textf2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textf.delegate = self
textf1.delegate = self
textf2.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func textf1(sender: AnyObject) {
let result = (textf.text! as NSString).doubleValue * (textf2.text! as NSString).doubleValue
textf1.text = String(format:"%.2f", result)
}
@IBAction func segmentedcontrolAction(sender: AnyObject) {
if(segmentedcontrol.selectedSegmentIndex == 0)
{
textf2.text = "5";
}
else if(segmentedcontrol.selectedSegmentIndex == 1)
{
textf2.text = "10";
}
else if(segmentedcontrol.selectedSegmentIndex == 2)
{
textf2.text = "15";
}
}
答案 0 :(得分:1)
@IBAction func segmentedcontrolAction(sender: AnyObject)
{
if(segmentedcontrol.selectedSegmentIndex == 0)
{
textf2.text = "5";
}
else if(segmentedcontrol.selectedSegmentIndex == 1)
{
textf2.text = "10";
}
else if(segmentedcontrol.selectedSegmentIndex == 2)
{
textf2.text = "15";
}
let result = (textf.text! as NSString).doubleValue * (textf2.text! as NSString).doubleValue
textf1.text = String(format:"%.2f", result)
}