每次在嵌套条件swift ios中都会弹出AlertController
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每次在嵌套条件swift ios中都会弹出AlertController相关的知识,希望对你有一定的参考价值。
当用户名或密码不正确时,我已经定义了一个alertcontroller警报应该弹出,并且它工作正常。但是当用户名和密码匹配时,尽管匹配,每次登录时都会弹出。我想我没有以正确的方式定义嵌套条件?帮我排序多个嵌套条件。
登录代码
import UIKit
import CoreData
import Foundation
class ViewController: UIViewController {
var usernameGlobal : String = ""
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passText: UITextField!
@IBOutlet weak var loginButton: UIButton!
@IBAction func loginAction(_ sender: Any) {
let appDel = UIApplication.shared.delegate as! AppDelegate
let context = appDel.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.returnsObjectsAsFaults = false
// request.predicate = NSPredicate(format: "username = %@", "" + emailText.text!)
do {
let results = try! context.fetch(request)
if(results.count > 0){
for result in results as! [NSManagedObject]
{
if emailText.text == result.value(forKey: "username") as? String && passText.text == result.value(forKey: "password") as? String {
print(emailText.text!, passText.text!)
usernameGlobal = self.emailText.text!
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}
else {
let alertController = UIAlertController(title: "Oops!", message: "Incorrect username or password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
}
}
}
@IBAction func signupSegue(_ sender: Any) {
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "SignupViewController") as! SignupViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
loginButton.backgroundColor = UIColor.blue
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案
您可以使用此代码:
@IBAction func loginAction(_ sender: Any) {
let appDel = UIApplication.shared.delegate as! AppDelegate
let context = appDel.persistentContainer.viewContext
let isMatched = false
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.returnsObjectsAsFaults = false
do {
let results = try! context.fetch(request)
if(results.count > 0){
for result in results as! [NSManagedObject]
{
if emailText.text == result.value(forKey: "username") as? String && passText.text == result.value(forKey: "password") as? String {
print(emailText.text!, passText.text!)
usernameGlobal = self.emailText.text!
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
isMatched = true
break
}
}
if !isMatched {
let alertController = UIAlertController(title: "Oops!", message: "Incorrect username or password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
}
}
变化:
你添加
let isMatched = false
在if条件中添加break语句。因此,一旦用户名和密码匹配,它就会打破循环。然后在for循环检查之后isMatched是否为false,然后显示结果。
另一答案
如果您的结果包含多个不同的元素,那么else将执行多次。这就是为什么警报多次出现。当你找到一场比赛然后你应该回来。如果没有找到匹配,那么循环警报将出现一次。
用这个:
@IBAction func loginAction(_ sender: Any) {
let appDel = UIApplication.shared.delegate as! AppDelegate
let context = appDel.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.returnsObjectsAsFaults = false
// request.predicate = NSPredicate(format: "username = %@", "" + emailText.text!)
do {
let results = try! context.fetch(request)
if(results.count > 0){
for result in results as! [NSManagedObject]
{
if emailText.text == result.value(forKey: "username") as? String && passText.text == result.value(forKey: "password") as? String {
print(emailText.text!, passText.text!)
usernameGlobal = self.emailText.text!
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
revealViewController().pushFrontViewController(newFrontViewController, animated: true)
return
}
}
let alertController = UIAlertController(title: "Oops!", message: "Incorrect username or password", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(defaultAction)
present(alertController, animated: true, completion: nil)
}
}
}
以上是关于每次在嵌套条件swift ios中都会弹出AlertController的主要内容,如果未能解决你的问题,请参考以下文章
我想创建 chrome 扩展,每次 youtube 播放视频时都会弹出
resignFirstResponder 没有按预期工作。每次按键时键盘都会弹出,而不是仅返回
Spring Security (4.0.1) 与 AngularJS 的集成。每次用户输入无效凭据时都会弹出基本身份验证