swift 如何修改MVP体系结构中使用的功能?

ldioqlga  于 2023-01-12  发布在  Swift
关注(0)|答案(1)|浏览(126)

我有下面的功能,可以正常工作。
当用户键入任何字符时,它都会验证用户输入,并根据输入隐藏一些imageView。

@IBAction func onEmailValueChanged(_ sender: UITextField) {
    let hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: sender.text!)
    passLengthCheckmarkImageView.isHidden = hasMinimumLength ? false : true
    let hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: sender.text!)
    passHasUppercaseCheckmarkImageView.isHidden = hasCapitalLetter ? false : true
    let hasNumber = TextValidationHelper.validateHasNumber(password: sender.text!)
    passHasNumberCheckmarkImageView.isHidden = hasNumber ? false : true
    let hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: sender.text!)
    passHasSymbolCheckmarkImageView.isHidden = hasSpecialCharacter ? false : true
    
    resetButton.isHidden = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true 
}

但是现在我想在这个函数上应用MVP模型,从ViewController文件中删除这个函数,我该怎么做呢?
我是否需要发布更多代码才能为这个问题创建答案?

tzcvj98z

tzcvj98z1#

只对方法使用任何架构模式都不是一个好的实践,所以假设你有一个包含许多类或文件的完整应用程序。
重要的一点是,使用任何特定的模式并不是固定的/强制的。它实际上取决于代码,有时候你会为了处理一个方法而写很多代码。所以试着想一个最佳的方法来使代码更具可测试性和可伸缩性。
但是为了便于参考,您可以检查以下代码:
在视图控制器上:

lazy var presenter:Presenter = Presenter(view:self)

 @IBAction func onEmailValueChanged(_ sender: UITextField) {
    presenter.validateHasMinimumLength(password: sender.text!)
    presenter.validateHasCapitalLetter(password: sender.text!)
    presenter.validateHasNumber(password: sender.text!)
    presenter.validateHasSpecialCharacter(password: sender.text!)
}

//采用视图控制器:视图控制器上的PrensenterView协议

extension ViewController:PrensenterViewProtocol {

func updateLengthCheckmarkImageView(isHidden:Bool) {
    passLengthCheckmarkImageView.isHidden = isHidden
}

func updateUpperCaseCheckmarkImageView(isHidden:Bool) {
    passHasUppercaseCheckmarkImageView.isHidden = isHidden
}

func updateNumberCheckmarkImageView(isHidden:Bool) {
    passHasNumberCheckmarkImageView.isHidden = isHidden
}

func updateSymbolCheckmarkImageView(isHidden:Bool) {
    passHasSymbolCheckmarkImageView.isHidden = isHidden
}

func updateResetButton(isHidden:Bool) {
    resetButton.isHidden = isHidden
}

}

演示者查看协议为:

protocol PrensenterViewProtocol:NSObjectProtocol {
    func updateLengthCheckmarkImageView(isHidden:Bool)
    func updateUpperCaseCheckmarkImageView(isHidden:Bool)
    func updateNumberCheckmarkImageView(isHidden:Bool)
    func updateSymbolCheckmarkImageView(isHidden:Bool)
    func updateResetButton(isHidden:Bool)
}

主讲人:

class Presenter {
weak var view:PrensenterViewProtocol!
private var hasMinimumLength:Bool = false
private var hasCapitalLetter:Bool = false
private var hasNumber:Bool = false
private var hasSpecialCharacter:Bool = false

init(view:PrensenterViewProtocol) {
    self.view = view
}
    
func validateHasMinimumLength(password:String?) {
    hasMinimumLength = TextValidationHelper.validateHasMinimumLength(password: password)
    self.view.updateLengthCheckmarkImageView(isHidden: hasMinimumLength)
    checkAllValidations()
}

func validateHasCapitalLetter(password:String?) {
    hasCapitalLetter = TextValidationHelper.validateHasCapitalLetter(password: password)
    self.view.updateUpperCaseCheckmarkImageView(isHidden:hasCapitalLetter )
    checkAllValidations()
}

func validateHasNumber(password:String?) {
    hasNumber = TextValidationHelper.validateHasNumber(password: password)
    self.view.updateNumberCheckmarkImageView(isHidden: hasNumber)
    checkAllValidations()
}

func validateHasSpecialCharacter(password:String?) {
    hasSpecialCharacter = TextValidationHelper.validateHasSpecialCharacter(password: password)
    self.view.updateSymbolCheckmarkImageView(isHidden: hasSpecialCharacter)
    checkAllValidations()
}

func checkAllValidations() {
    let areAllValid:Bool = hasMinimumLength && hasCapitalLetter && hasNumber && hasSpecialCharacter ? false : true
    self.view.updateResetButton(isHidden: areAllValid)
}

}

相关问题