我是Swift iOS开发的新手,我正在创建用户登录页面,当他们点击Sign In
按钮时,会对用户输入执行一些验证,如果成功,则将用户发送到新页面。在故事板中,我有两个视图控制器,登录页面和登录成功后的页面。我已经从登录页面上的Sign In
按钮创建了一个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
}
}
此代码在登录后成功运行并成功将用户发送到正确的页面。但是当我添加一个说明"Success"
的警报时,警报关闭后不会加载新页面。以下是添加成功登录警报的代码。代码在函数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)
我没有被发送到这个警报的新页面的原因是什么?如果"Success"
警报被注释掉,我就会被发送到正确的页面。
我正在使用Xcode 9
和Swift 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)