简体   繁体   中英

UIButton inside UISegmentedControl action

I am continuing my calculation program to improve my skills in Swift code. 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.

Here is the code but is not working.

Thanks, 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.

I think your @IBAction func operatorSelectorChanged should look something like this:

@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 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)

I created an example project that illustrates this: https://github.com/dev1an/calculator


How to bind multiple events to one IBAction

Write down the IBAction in code (like the calculate action I wrote above). Go to your storyboard and open the assistent editor ( Alt Cmd Enter ). Select a UI element (like a text field) in the interface builder. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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