简体   繁体   English

UISegmentedControl动作内的UIButton

[英]UIButton inside UISegmentedControl action

I am continuing my calculation program to improve my skills in Swift code. 我正在继续我的计算程序,以提高我在Swift代码中的技能。 Right now I have a problem: I want that an UISegmentedControl select the operator of my operations (+, - ecc..) and when one is selected a UIButton func calculate the values based on the decision of the UISegmentedControl. 现在,我遇到一个问题:我希望UISegmentedControl选择我的操作的运算符(+,-ecc ..),并且当选择一个操作符时,UIButton函数会根据UISegmentedControl的决定来计算值。

Here is the code but is not working. 这是代码,但不起作用。

Thanks, Matteo 谢谢,Matteo

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var firstNumber: UITextField!
@IBOutlet weak var secondNumber: UITextField!
@IBOutlet weak var resultButton: UIButton!
@IBOutlet weak var resultNumber: UILabel!
@IBOutlet weak var operatorSelector: UISegmentedControl!

override func viewDidLoad() {
    super.viewDidLoad()
    // 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 operatorSelectorChanged(_ sender: UISegmentedControl) {
    switch operatorSelector.selectedSegmentIndex {
    case 0:
        @IBAction func resultFunction(_ sender: AnyObject) {
            if let firstNumberConv :Int = Int(firstNumber.text!), let secondNumberConv :Int = Int(secondNumber.text!) {
                let result = firstNumberConv + secondNumberConv
                resultNumber.text = "\(result)"
            } else { resultNumber.text = "Inserire solo valori numerici"}
        }


    case 1:
        @IBAction func resultFunction(_ sender: AnyObject) {
            if let firstNumberConv :Int = Int(firstNumber.text!), let secondNumberConv :Int = Int(secondNumber.text!) {
                let result = firstNumberConv - secondNumberConv
                resultNumber.text = "\(result)"
            } else { resultNumber.text = "Inserire solo valori numerici"}
        }

    default:
        break;

    }

}



}

Try removing the lines with @IBAction func ... inside your switch cases. 尝试在开关盒内使用@IBAction func ...删除行。

I think your @IBAction func operatorSelectorChanged should look something like this: 我认为您的@IBAction func operatorSelectorChanged应该看起来像这样:

@IBAction func operatorSelectorChanged(_ sender: UISegmentedControl) {
    switch operatorSelector.selectedSegmentIndex {
    case 0:
        if let firstNumberConv = Int(firstNumber.text!), let secondNumberConv = Int(secondNumber.text!) {
            let result = firstNumberConv + secondNumberConv
            resultNumber.text = "\(result)"
        } else { resultNumber.text = "Inserire solo valori numerici"}


    case 1:
        if let firstNumberConv = Int(firstNumber.text!), let secondNumberConv = Int(secondNumber.text!) {
            let result = firstNumberConv - secondNumberConv
            resultNumber.text = "\(result)"
        } else { resultNumber.text = "Inserire solo valori numerici"}

    default:
        break;

    }

}

A possible solution for your problem is to define an IBAction that calculates the result of adding/subtracting two numbers 您的问题的可能解决方案是定义一个IBAction来计算两个数字相加/相减的结果

@IBAction func calculate(_ sender: AnyObject) {
    if let a = Int(firstNumber.text!), let b = IntsecondNumber.text!) {
        switch operatorSelector.selectedSegmentIndex {
        case 0:
            resultNumber.text = "\(a+b)"
        case 1:
            resultNumber.text = "\(a-b)"
        default:
            print("❗️unknown operator selected")
        }
    }
}

You could then bind this action to the sent events from your user interface: for example the Editing Changed event of your text fields (firstNumber and secondNumber) and the Value Changed of your segmented control (operatorSelector) 然后,您可以将此操作绑定到从用户界面发送的事件:例如,文本字段(firstNumber和secondNumber)的Editing Changed事件和分段控件的Value Changed (operatorSelector)

I created an example project that illustrates this: https://github.com/dev1an/calculator 我创建了一个示例项目来说明这一点: https : //github.com/dev1an/calculator


How to bind multiple events to one IBAction 如何将多个事件绑定到一个IBAction

Write down the IBAction in code (like the calculate action I wrote above). 用代码写下IBAction(就像我上面写的calculate动作一样)。 Go to your storyboard and open the assistent editor ( Alt Cmd Enter ). 转到情节提要,然后打开辅助编辑器( Alt Cmd Enter )。 Select a UI element (like a text field) in the interface builder. 在界面构建器中选择一个UI元素(如文本字段)。 Open the Connections Inspector ( Alt Cmd 6 ) and drag from the circle next to a sent event (for example Editing Changed) to your IBAction code. 打开“连接”检查器( Alt Cmd 6 ),然后从已发送事件(例如,“ Edited Changed”)旁边的圆圈拖动到IBAction代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM