How to access viewController components in model (MVC)

Hello, For my third iOS project, I want to use the MVC design pattern. Until now the logic of my application was in viewController. My project is a calculator. I want to put the logic in a class named :" Compute " and manage the buttons in viewControler. But the problem is that I need to use the UIAlertController and the textView in the Compute class but I don't have access. Here is some of my code the Compute class is empty for the moment.


    // MARK: - Outlets

    @IBOutlet weak var textView: UITextView!

    @IBOutlet var numberButtons: [UIButton]!

    // MARK: - Propriétés

    var compute = Compute()

    

    var elements: [String] {

        return textView.text.split(separator: " ").map { "\($0)" }

    }

    // Error check computed variables

    var expressionIsCorrect: Bool {

        return elements.last != "+" && elements.last != "-"

    }

    //

    var expressionHaveEnoughElement: Bool {

        return elements.count >= 3

    }

    //

    var canAddOperator: Bool {

        return elements.last != "+" && elements.last != "-"

    }

    //

    var expressionHaveResult: Bool {

        return textView.text.firstIndex(of: "=") != nil

    }

    // View Life cycles

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

    }

    // MARK: - actions

    @IBAction func tappedNumberButton(_ sender: UIButton) {

        guard let numberText = sender.title(for: .normal) else {

            return

        }

        if expressionHaveResult {

            textView.text = ""

        }

        textView.text.append(numberText)

    }

    @IBAction func tappedAdditionButton(_ sender: UIButton) {

        if canAddOperator {

            textView!.text.append(" + ")

        } else {

            let alertVC = UIAlertController(title: "Zero!", message: "Already have an operator !", preferredStyle: .alert)

            alertVC.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))

            self.present(alertVC, animated: true, completion: nil)

        }

    }

the problem is that I need to use the UIAlertController and the textView in the Compute class

That's against the MVC design pattern. You should not touch views directly from Model.

You need to better understand what MVC is.

The model is somehow the back office: you ask it to do some computation, to return you values to display. But it has no access to the front office (the view content)

The view is presenting the data that it can get from the Model.

And Controller manages the logic and connects the 2. That also where you trigger alerts.

So, you should:

  • in VC, when the user taps, you request the model to update some value
  • on return, you pass those values to the view (which usually means updating the IBOutlets)

You may have a look at this tutorial: https :// medium. com/@adam_50439/coordinate-dont-delegate-59a04f97996c

Okay, and do you have any idea how I can do that? Or a resource, a link to help me? 

You can make your Model to trigger some events, and your view controller will be able to show UIAlertController or update text view based on the evens.

To achieve this, you can use NotificationCenter, property observer, delegate pattern or there may be many other ways. Combine would be your another option if you prefer some modern ways.

But, as you say Compute class is empty, I cannot say what would be best for you and cannot show any examples.

How to access viewController components in model (MVC)
 
 
Q