Custom View TextField How to calculate the amount of TextField

I am working on an iOS application in swift 3.0, where I create a custom view with a text field, and the button calculates the values ​​for the entire text file and displays the sum of the entire text field in the upper resulting text.

Code for MainViewController:

@IBOutlet weak var totalText: UITextField!
var totalview:[UIView]!

override func viewDidLoad() {
    yvalue = 1
    tag = 1
    count = 1
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func actionButton(_ sender: UIButton) {
    yvalue = 55 + yvalue
    //for i in 0...count {
        extraview = View(frame: CGRect(x: 50, y: 75+yvalue, width: 350, height: 50))
        extraview.backgroundColor = UIColor(white: 1, alpha: 0.5)
        extraview.layer.cornerRadius = 15
        extraview.tag = tag
        print("ExtraView tag=",extraview.tag)
        extraview.ActionButtonsub.addTarget(self, action: (#selector(cancelbutton(_:))), for: UIControlEvents.touchUpInside)
        extraview.textFiled.addTarget(self, action: #selector(didChangeTexts(textField:)), for: .editingChanged)
        extraview.textFiled.tag = tag
        print("text tag=",extraview.textFiled.tag)
        self.view.addSubview(extraview)
        count = count + 1
        tag = tag + 1
    //}
}

func cancelbutton(_ sender: UIButton) {
    extraview.removeFromSuperview()
}

func didChangeTexts(textField: UITextField) {
        totalText.text = extraview.textFiled.text
}

Code for UIView:

class View: UIView {

    @IBOutlet var subView: UIView!
    @IBOutlet weak var textFiled: UITextField!
    @IBOutlet weak var ActionButtonsub: UIButton!

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        Bundle.main.loadNibNamed("View",owner: self, options:nil)
        self.addSubview(self.subView)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        Bundle.main.loadNibNamed("View", owner: self, options: nil)
        subView.frame = bounds
        self.addSubview(self.subView)
    }
}

Output example

+4
source share
3 answers

Something like this should work, a little modification may be required.

You just need to iterate through the subviews, get the text box for each view, convert to a double value and add them along the way.

func calculateTotal() -> Double {
   var total: Double = 0.0
   for subview in self.view.subviews {
      if let v = subview as? View, !v.textFiled.isEmpty {
          total += Double(v.textFiled.text)
      }
   }

   return total
}
0
source

, @Scriptable , , .

, , , - .

0

An alternative is to use functions filter, flatMapandreduce

let sum = (view.subviews.filter{$0 is View} as! [View]) // filters all `View` instances
    .flatMap{$0.textFiled.text} // maps all text properties != nil
    .flatMap{Double($0)} // maps all values which are convertible to Double
    .reduce(0.0, {$0 + $1}) // calculates the sum
0
source

Source: https://habr.com/ru/post/1676901/


All Articles