gpt4 book ai didi

swift - 登录失败导致未编码的转场?

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:58 25 4
gpt4 key购买 nike

我设置了一个应用程序,用户可以在其中登录和注销(这是通过 heroku 托管的 Parse-server 处理的,带有 Parse 框架)。

要登录,用户输入凭据,如果服务器验证了它们,segue 会将它们移动到另一个 View Controller 。注销会将用户从应用程序中退出,然后返回到登录屏幕。这些工作正常。

如果初始登录凭据不正确,则会显示一条警报,提示用户重试,并且用户未登录(显然)并留在登录屏幕上。

但是,如果用户登录、注销,然后输入不正确的凭据(按此顺序,没有关闭模拟器),就会出现问题。不正确的凭据会弹出提示警报;点击“确定”关闭警报,用户未登录,但 View 被转到注销屏幕(注销按钮在哪里......这应该只有登录用户可以访问)。不存在将用户从登录直接移动到注销屏幕的 segue segue(无论是编码的还是在 Storyboard上)。

因为许多操作需要用户 ID,所以没有任何操作有效,而且没有任何操作,因为没有人登录。我不知道为什么不正确的登录会以这种方式移动 VC,因为不存在可以做到这一点的 segue。有一个反向移动(注销时),但没有朝那个方向移动。

有人有什么想法吗?下面是相关的登录代码。

@IBAction func signInTapped(_ sender: UIButton) {
UIApplication.shared.beginIgnoringInteractionEvents()

PFUser.logInWithUsername(inBackground: userNameTextField.text!, password: passwordTextField.text!, block: { (user, error) in
if user != nil{

self.performSegue(withIdentifier: "signInSegue", sender: self)//if the sign in works
} else{

if let errorString = (error! as NSError).userInfo["error"] as? String{

let errorMessage = errorString

self.displayAlert("Failed login", message: errorMessage)//this is the code run when the login fails, yet it somehow can sometimes segue to the other viewcontroller
}
}
})
}

这是正在发生的事情的分步示意图。 The user starts at the login page and inputs his/her credentials This segues the user (now logged on) to a <code>tabbarViewController</code> They then navigate to another tab and there is a sign out button Pressing the sign out button causes an alert to appear, hitting yes segues the user (now logged out) to the original screen The user is now at the login screen. Inputting the wrong info brings up an alert The alert appears. Hitting yes closes the alert is supposed to leave the user on the login screen. However, there is an immediate segue to the logout screen again... this is neither coded nor manually set in the storyboard

  1. 用户从登录页面开始并输入他/她的凭据
  2. 这会将用户(现在已登录)转到 tabbarViewController
  3. 然后他们导航到另一个选项卡,并且有一个退出按钮
  4. 按下注销按钮会导致出现警报,点击是会将用户(现在已注销)转到原始屏幕
  5. 用户现在位于登录屏幕。输入错误的信息会发出警报
  6. 出现警报。点击是关闭警报应该让用户留在登录屏幕上。但是,再次立即转到注销屏幕......这既没有编码也没有在 Storyboard 中手动设置

我认为可能相关的随机事件:打开应用程序时,会在第一个 VC(登录屏幕)的 viewDidAppear 上调用一个初始 segue。它检查是否有用户登录,通过

if PFUser.current().username? != nil {
//perform segue
}

此 segue 将他们带到选项卡栏上的 map 部分(而不是出现问题时出现的注销页面)。

编辑:在出现“segue”到注销屏幕时,登录屏幕下降(就像字面意思一样,向下落在屏幕上直到它看不见,然后注销屏幕是可见的,这不是任何 segues 中的方式应用程序功能)。此外,只有 viewDidAppear 代码在发生此转换时被调用,不是 viewDidLoad 代码。

编辑 2:我认为问题与 View 层次结构有关。这是登录功能的代码:

PFUser.logInWithUsername(inBackground: userNameTextField.text!, password: passwordTextField.text!, block: { (user, error) in

if user?.username != nil{
self.performSegue(withIdentifier: "signInSegue", sender: self)
} else{ //if everything in this code block is commented, the issue does not occur
if let errorString = (error! as NSError).userInfo["error"] as? String{
let errorMessage = errorString
self.displayAlert("Failed login", message: errorMessage)
}
}
})

如果我注释掉 if 语句的 else 部分中的所有内容,问题就不会发生。我认为正在发生的是显示警报,然后点击“确定”关闭警报并将应用程序发送回最顶层(也许我对它的调用是错误的)viewController,这在某种程度上是注销 VC .我认为解决它的一个好方法是将 self.present(homescreenVC) 或类似的东西添加到 else 代码中,但我不知道如何这样做。

最佳答案

@IBAction func signInTapped(_ sender: UIButton) {
UIApplication.shared.beginIgnoringInteractionEvents()

PFUser.logInWithUsername(inBackground: userNameTextField.text!, password: passwordTextField.text!, block: { (user, error) in
if error != nil {
if let errorString = (error! as NSError).userInfo["error"] as? String{

let errorMessage = errorString

self.displayAlert("Failed login", message: errorMessage)
}
}
else {
self.performSegue(withIdentifier: "signInSegue", sender: self)

}
})
}

关于swift - 登录失败导致未编码的转场?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41458601/

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