H
H
Hirako7972018-12-27 20:29:30
Swift
Hirako797, 2018-12-27 20:29:30

How to pass variable value from UIPickerView to ViewController?

Created a UIPickerView in a separate .swift file

import UIKit
@IBDesignable class BorderedView : UIView{
    /// Толщина границы
    @IBInspectable var borderWidth: CGFloat {
        set { layer.borderWidth = newValue }
        get { return layer.borderWidth }
    }
    /// Цвет границы
    @IBInspectable var borderColor: UIColor? {
        didSet { layer.borderColor = borderColor?.cgColor }
    }
    /// Радиус границы
    @IBInspectable var cornerRadius: CGFloat {
        set { layer.cornerRadius = newValue }
        get { return layer.cornerRadius  }
    }
}
class SelectAngleViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    @IBOutlet weak var anglePicker: UIPickerView!
    @IBOutlet weak var saveAngleButton: UIButton!
    
    var array1 = ["90°", "60°", "45°", "30°", "15°"]
    var label = "5"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        anglePicker.delegate = self
        anglePicker.dataSource = self
        view.backgroundColor = UIColor.clear
        view.isOpaque = false
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
        return array1[row]
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return array1.count
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            label = array1[row]
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    @IBAction func saveAngleButton(_ sender: UIButton) {
        dismiss(animated: true)
        let actualRow = self.anglePicker.selectedRow(inComponent: 0)
        label = array1[actualRow]
    } 
}

Next, you need to use the selected in the PickerView in the ViewController:
func sRoundTap (dTF: String, qTF: String) -> Double{
        var d: Double = 0
        var angle = ""
        var q : Double = 0
        
        if let dTF = dRoundTapTF.text {
            d = Double(dTF.replacingOccurrences(of: ",", with: ".")) ?? 0
        }
        if let qTF = qRoundTapTF.text {
            q = Double(qTF.replacingOccurrences(of: ",", with: ".")) ?? 0
        }
        let s = CalcAreaFitting.shared.areaTapRound(angle: angle, d: d, q: q)
        return s
    }
    @IBAction func dRoundTapTF(_ sender: UITextField) {
        let s = sRoundTap(dTF: dRoundTapTF.text ?? "0", qTF: qRoundTapTF.text ?? "0")
        sRoundTapLabel.text = "ХХХХХХХХХХ " + "\(s)" + " Х"
    }
    @IBAction func qRoundTapTF(_ sender: UITextField) {
        let s = sRoundTap(dTF: dRoundTapTF.text ?? "0", qTF: qRoundTapTF.text ?? "0")
        sRoundTapLabel.text = "ХХХХХХХХХХ: " + "\(s)" + " Х"
    }

But, unfortunately, the variable is visible only within the function @IBAction func saveAngleButton(_ sender: UIButton)
Please tell me: How can I move the variable outside of this function? How to pass it to ViewController.swift?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question