Greg
Greg

Reputation: 707

Read/Write UITexTField from another class

I have a view with several text fields that I would like to read from the viewController, validate, then call a function in another class in another file to write out the result.

I know I could return the results and write them from the ViewController, but some functions (not shown here) have so many input and output fields that it seems ridiculously complicated to pass them back out. How do I do this? Can I do it? (Or am I just trying to write really bad code?)

This is a simplified version of what I currently have:

MainViewController.swift:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var fieldLength1: UITextField!  
    @IBOutlet weak var fieldLength2: UITextField!
    @IBOutlet weak var fieldLength3: UITextField!
    @IBOutlet weak var fieldLength4: UITextField!    
    @IBOutlet weak var fieldPerimeter: UITextField!    
    @IBOutlet weak var fieldArea: UITextField!    

    override viewDidLoad() {
        super.viewDidLoad()

        fieldLength1.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        fieldLength2.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        fieldLength3.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        fieldLength4.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)

    }

    @objc func textFieldDidChange() _ textField: UITextField) {
        if isValidEntry(textField: textField) {
            // Here is where I would like to call the function in the other class
            // Something like:
            // MathFunc().perimeter(
            // MathFunc().area
        }
    }

    func isValidEntry() {
        // ensure entries are only numbers and that enough info is present for calculations
    }
}

MathFunctions.swift:

import Foundation

class MathFunctions {
    func perimeter(l1: Double, l2:Double, l3:Double, l4:Double) -> Double {
        let p = l1 + l2 + l3 + l4    
        // Here is where I would like to write the result to the UITextField
        // Something like:
        // fieldPerimeter.text = p //with some formatting not shown here
    }

    func area(width: Double, length:Double) -> Double {
        let rectArea = width * length 
        // Here is where I would like to write the result to the UITextField
        // Something like:
        // fieldArea.text = rectArea //with some formatting not shown here
    }
}

Upvotes: 0

Views: 241

Answers (2)

Daniel T.
Daniel T.

Reputation: 33967

Firstly, it looks to me that fieldPerimeter and fieldArea should be labels, not text fields. They are output, not input.

Secondly, remember that area(width:length:) returns the result. Call the function in the view controller and the push the result into the field.

The View Controller is suppose to be the thing that pushes values into labels.

Upvotes: 1

matt
matt

Reputation: 534977

Change your MathFunctions to a struct, and change its functions to static. Now you can say MathFunctions.perimeter() from anywhere.

Upvotes: 2

Related Questions