每次在嵌套条件swift ios中都会弹出AlertController(示例代码)

栏目: ios · 发布时间: 2021-05-06

简介  这篇文章主要介绍了每次在嵌套条件swift ios中都会弹出AlertController(示例代码)以及相关的经验技巧,文章约7150字,浏览量262,点赞数6,值得推荐!

当用户名或密码不正确时,我已经定义了一个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)

                }
            }
        }

以上就是本文的全部内容,希望对大家的学习有所帮助,本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

“你希望应用程序.ios.app接受传入的网络连接”每次都弹出diaplay

Linux Shell编程(17)——嵌套循环(示例代码)

Win10系统下点击宽带连接会弹出设置界面的解决方法

如何解决UIImageView连接到Swift4? [关闭]

iOS Swift如何打开位置权限弹出窗口(示例代码)

自学Linux Shell12.6-嵌套循环for命令(示例代码)

Macbook 修复Office Excel 异常问题(示例代码)

Swift:如何以编程方式获取iOS设备的唯一标识符?