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)
}
}