如何在MVVM中使用UIAlertController?

时间:2017-08-31 14:24:55

标签: ios swift mvvm uialertcontroller

我有一个带代码的VC来显示警告:

func showMessage() {
    let alertView = UIAlertController(title: "TEST",
                                      message: self.loginViewModel.errorText,
                                      preferredStyle: .alert)
    alertView.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: nil))
    present(alertView, animated: true, completion: nil)
}

我在viewModel中有这个登录逻辑,它需要触发这个函数:

func submitLoginRequest(userLogin: String, loginPassword: String, loginSecret: String, deviceToken: String) {
        let userLogin = UserServices.init()
        manager.userServicesApiRequest(url: Endpoints.login, request: userLogin) { (data, error) in
            if let data = data {
                let status = data["status"].stringValue
                if status == "success" {
                    guard let userObject = UserProfileModel.init(data) else { return }
                    let encodedUserObject: Data = NSKeyedArchiver.archivedData(withRootObject: userObject)
                    UserDefaults.standard.set(encodedUserObject, forKey: "userProfile")
                    print("Login Succeeded")                        self.coordinatorDelegate?.loginViewModelDidLogin(viewModel: self)
                } else {
                    self.errorText = data["reason"].stringValue
                    // here is where the controller needs calling!
                }
            }

我想知道当VM案件被击中时我应该如何正确地进行交互以触发VC?

0 个答案:

没有答案