gpt4 book ai didi

swift - 验证按钮单击并更改 Controller View iOS

转载 作者:行者123 更新时间:2023-11-30 11:10:23 24 4
gpt4 key购买 nike

我是 Swift iOS 开发新手,我正在创建一个用户登录页面,当他们单击登录按钮时,会对用户输入执行一些验证,如果成功,将用户发送到新页面。在 Storyboard中,我有两个 View Controller ,登录页面和登录成功后的页面。我已经创建了从登录页面上的登录按钮到第二页的segue。这是以下代码。

@IBAction func loginAction(_ sender: Any) {
guard let username = usernameField.text, let password = passwordField.text else {
return
}
if (username.isEmpty && password.isEmpty) {
displayAlert(message: "username and password are not set")
return
}
if (username.isEmpty) {
displayAlert(message: "username is not set")
return
}
if (password.isEmpty) {
displayAlert(message: "password is not set")
return
}
}

此代码有效,并在登录后成功将用户发送到正确的页面。但是,当我添加显示“成功”的警报时,警报启动后不会加载新页面。关闭。以下是添加登录成功警报的代码。该代码添加在函数 loginAction 中的最终条件之后。

let alert = UIAlertController(title: "Success", message: "Sucessfully logged in", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)

为什么我没有被发送到带有此警报的新页面?如果“成功”警报被注释掉,我将被发送到正确的页面。

我使用的是 Xcode 9Swift 4

编辑:这是代码的类。

class ViewController: UIViewController {
@IBOutlet weak var usernameField: UITextField!
@IBOutlet weak var passwordField: UITextField!

@IBAction func loginAction(_ sender: Any) {
guard let username = usernameField.text, let password = passwordField.text else {
return
}
if (username.isEmpty && password.isEmpty) {
displayAlert(message: "username and password are not set")
return
}
if (username.isEmpty) {
displayAlert(message: "username is not set")
return
}
if (password.isEmpty) {
displayAlert(message: "password is not set")
return
}
login(info: username)
// self.present(alert, animated: true, completion: nil)
// self.present(self, animated: true) {
// let alert = UIAlertController(title: "Success", message: "Sucessfully logged in with \(username)", preferredStyle: UIAlertControllerStyle.alert)
// alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
// self.present(alert, animated: true, completion: nil)
// }
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

private func login(info: String) {
let alert = UIAlertController(title: "Success", message: "Sucessfully logged in with \(info)", preferredStyle: UIAlertControllerStyle.alert)
// alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default) { action in self.dismiss(animated: true) })
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}

private func displayAlert(message: String) {
let alert = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}

最佳答案

为了让它正常工作,我需要这样做。我更新了我的函数 loginAction

let alert = UIAlertController(title: "Success", message: "Sucessfully logged in with \(info)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.default) { action in
self.performSegue(withIdentifier: "loginSegue", sender: nil)
})
self.present(alert, animated: true, completion: nil)

关于swift - 验证按钮单击并更改 Controller View iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52250090/

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