如果segue.identifier ==" showChildFiles" swift 3准备segue不通过当segue标识符看似正确时

时间:2017-03-29 02:15:33

标签: ios uiviewcontroller swift3 segue

我正在创建一个应用程序,父母可以登录并查看有关当地幼儿中心的信息。但是,运行视图控制器以进行登录的代码无法正常工作。我希望我的代码使用标识符" ShowChildFiles"来执行segue。一旦用户按下登录按钮。但是在准备(对于segue:UIStoryboardSegue,sender:Any?)函数中,我检查了正确的segue标识符(如果segue.identifier ==" ShowChildFiles")但是它没有传递这个if语句。我在标识失败时打印出标识符并返回" ShowChildFiles"。

这是代码

import Foundation
import UIKit

class MyChildLoginViewController: UIViewController {

    @IBOutlet weak var usernameTextField: UITextField!

    @IBOutlet weak var passwordTextField: UITextField!

    @IBAction func loginButtonPressed(_ sender: Any) {

        var request = URLRequest(url: URL(string: "myLogin.php")!)
        request.httpMethod = "POST"
        let postString = "userName=" + usernameTextField.text! + "&password=" + passwordTextField.text!
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {                                                 // check for fundamental networking error
                print("error=\(error)")
                return
            }

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }

            let responseString = String(data: data, encoding: .utf8)
            print("responseString = \(responseString)")

            if(responseString == "true"){
                //Login successful
                DispatchQueue.main.async(//Not sure why but this has to be executed asynchronously
                    execute: {
                        self.performSegue(withIdentifier: "ShowChildFiles", sender: Any?.self)
                    })

            }else if(responseString == "false"){
                //incorrect password
            }else if(responseString == "incorrect username"){
                //We dont recognise this username
            }else{
                //something went wrong
            }
        }
        task.resume()
    }

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

        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyChildLoginViewController.dismissKeyboard))//Dismisses keyboard when screen tapped
        view.addGestureRecognizer(tap)
    }

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

     // MARK: - Navigation
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.

        if  segue.identifier == "ShowChildFiles" ,
            let destination = segue.destination as? ChildFilesTableViewController {
            print("preparing for segue")
            destination.passedString = self.getFamID()
        }else{
            print(segue.identifier!)
            print("not going in the correct place")
        }

     }

    func getFamID() -> String{

        let cs: [Character] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o","p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "]
        /*var cs = Set(chars.characters)*/
        var user = [Character](usernameTextField.text!.lowercased().characters)
        var famID = ""
        for index in 0 ... user.count - 1 {
            if(!cs.contains(user[index])){
                famID = famID + String(user[index])
            }
        }
        return famID

    }
    //Calls this function when the tap is recognized.
    func dismissKeyboard() {
        //Causes the view (or one of its embedded text fields) to resign the first responder status.
        view.endEditing(true)
    }
}

1 个答案:

答案 0 :(得分:2)

由于您的日志记录似乎证明了segue标识符是您认为的"ShowChildFiles" - 实际上,它几乎不可能,因为这是您用于{{1}的标识符首先是segue - 必须得出结论,perform条件失败的部分是 second 部分if。很显然,segue的目的地不是一个ChildFilesTableViewController。 (为什么不将let destination = segue.destination as? ChildFilesTableViewController作为记录的一部分,并找出来?)