Home > other >  How to check text field changes and send them to model function? MVC / Swift 5
How to check text field changes and send them to model function? MVC / Swift 5

Time:08-15

The question is about MVC. I need to check my text field changes, when user typing some text. Text field is in main view controller and I need to send changes to model function which is in another class in another swift file. Please tell me the correct way how I can do this correct in MVC?) What should I use in my VC and Model classes?)


Using in ViewController:

import UIKit

class ViewController: UIViewController {

private let reverseWords = ReverseWords()

//MARK: IBOutlets
@IBOutlet weak var reverseTextField: UITextField!
@IBOutlet weak var exeptionTextField: UITextField!
@IBOutlet weak var resultLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    
    //MARK: Delegation
    reverseTextField.delegate = self
    exeptionTextField.delegate = self
    
}

//I am currently using this for checking my exeptionTextField.
//But want to move it to a model.
//How can I check exeptionTextField there?
public func reverseWithIgnoringLetters() {
    
    guard let textForReverse = reverseTextField.trimmedTextNilIfEmpty,
          let textForExeption = exeptionTextField.trimmedTextNilIfEmpty else {
        resultLabel.text = ""
        return
    }
    
    resultLabel.text = "  "   textForReverse.components(separatedBy: " ").map {
        $0.reversed(omittingCharactersIn: CharacterSet(charactersIn: textForExeption)) }.joined(separator: " ")
}

@IBAction func exceptionTextFieldEditingChanged(_ sender: UITextField) {
    
    if segmentController.selectedSegmentIndex == 1 && exeptionTextField.hasText {
        reverseWithIgnoringLetters()
    } else {
        resultLabel.text = "  "   reverseWords.segmentedControlByCustom(
            textField: reverseTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines))
    }

Using in model:

import UIKit
import Foundation

class ReverseWords {

public func segmentedControlByCustom(textField: String ) -> String {
    if textField.isEmpty {
        return ""
    }
    return textField.trimmingCharacters(in: .whitespacesAndNewlines)
        .components(separatedBy: " ")
        .map { String ( $0.reversed() ) }
        .joined(separator: " ")
}
}

//MARK: Exclusions extension for exepsion text field
extension String {
func reversed(omittingCharactersIn characterSet: CharacterSet) -> String {
    var reversed = reversed()
        .filter { String($0).rangeOfCharacter(from: characterSet) == nil }
    let excluded = enumerated()
        .filter { String($0.element).rangeOfCharacter(from: characterSet) != nil }
    for (i, char) in excluded {
        reversed.insert(char, at: reversed.index(reversed.startIndex, offsetBy: i))
    }
    return String(reversed)
}
}

CodePudding user response:

You can check textfield changes by implementation textField delegate method

 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { }

You can update model which is in different class by using delegate OR closure OR using Notification Center.

CodePudding user response:

At the risk of over-stating the obvious:

MVC = Model View Controller.

Your text field is your view object.
You view controller object is your controller.
Your model is your model.

The text field delegate methods allow you view controller to collect changes from your view object and install those changes in the model. So do that. Implement textField(_:shouldChangeCharactersIn:replacementString:) and other methods that let the user change text) and have those methods send changes to your model via your "model function". (Your view controller will need a reference to your model object.)

  • Related