gpt4 book ai didi

ios - 显示来自模型的警报

转载 作者:行者123 更新时间:2023-11-28 12:39:56 24 4
gpt4 key购买 nike

我正在尝试从 Firebase 后端向我的用户显示错误警报。

我根据 MVC 分离了我的 Signup 函数并将其放入模型中,但是 print(error!.localizedDescription) 嵌套在我模型中的 if 语句 中.我不能简单地制作一个 alertController,因为它不属于 main.storyboard 中的 View 。

如何从我的模型中提取该错误并通过 VC 将其显示回 View ?

型号

func signUp(email: String, username: String, password: String, data: NSData!)
{

FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil
{

self.setUserInfo(user: user, username: username, password: password, data: data)

}
else
{
print(error!.localizedDescription)
}
})
}

查看 Controller

@IBAction func signupButton(_ sender: AnyObject)
{
if self.emailTextField.text == "" || self.usernameTextField.text == "" || self.passwordTextField.text == ""

{
let alertController = UIAlertController(title: "Oops!", message: "Please enter a valid username, email & password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alertController.addAction(defaultAction)

self.present(alertController, animated: true, completion: nil)
}
else
{
let data = UIImageJPEGRepresentation(self.userProfilePicture.image!, 0.8)

networkingService.signUp(email: emailTextField.text!, username: usernameTextField.text!, password: passwordTextField.text!, data: data as NSData!)
}
}

NetworkingService 是我调用的结构。

感谢您的帮助,我仍在努力研究 MVC。

最佳答案

向模型的 signUp 方法添加一个完成 block 。

func signUp(email: String, username: String, password: String, data: NSData, completion: Error? -> ())
{
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
self.setUserInfo(user: user, username: username, password: password, data: data)
}
completion(error)
})
}

然后您提供一个闭包,当用户创建/未创建时将调用该闭包。

@IBAction func signupButton(_ sender: AnyObject) {

guard
let email = emailTextField.text,
let username = usernameTextField.text,
let password = passwordTextField.text,
let image = userProfilePicture.image,
let data = UIImageJPEGRepresentation(image, 0.8)
where
email != "" &&
username != "" &&
password != ""
else {
let alertController = UIAlertController(title: "Oops!", message: "Please enter a valid username, email & password", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}

networkingService.signUp(email: email, username: username, password: password, data: data) { error in
if let error = error {
print(error.localizedDescription)
}
}
}

关于ios - 显示来自模型的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39692747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com