简体   繁体   English

Swift3和Xcode8:执行功能后无法执行Segue

[英]Swift3 and Xcode8: Not able to perform segue after execution of a function

I am trying to perform segue to Home Screen after checking whether entered username and password are correct and if they are incorrect I am trying to show an alert message. 在检查输入的用户名和密码是否正确以及它们是否不正确之后,我试图对主屏幕执行segue,我试图显示警报消息。 I am not successful in performing either of those. 我在执行任何一项操作上均不成功。 Because as soon as I click the login button a white screen is displayed. 因为一旦我单击登录按钮,就会显示白屏。 Below is my code: 下面是我的代码:

Performing segue in the following method: 通过以下方法执行segue:

@IBAction func didClickLogin(_ sender: Any) {
    self.progressIndicator.isHidden = false
    self.progressIndicator.startAnimating()
    let enteredUserName = mMobileNumber.text
    let enteredPassword = mPassword.text
    //performing validation for username and password
    didUserLogin(username: enteredUserName!, password: enteredPassword!)
    return true
}

My didLogin function: 我的didLogin函数:

func didUserLogin(username: String, password: String) {
    let requestString = Constants.BASE_URL+"/mlogin"
    let parameters: Parameters =  ["username":username, "password":password]
    let headers = ["Authorization": "Basic \(Constants.base64LoginString)"]

    Alamofire.request(requestString, method:.post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { response in
        //to get status code
        switch response.result {
        case .success(let data):
            let json = JSON(data)
            switch (json["status"].stringValue){
            case "200":
                DispatchQueue.main.async {
                    self.performSegue(withIdentifier: "loginSuccess", sender: self)
                }
                break

            case "401":
                self.progressIndicator.isHidden = true
                self.progressIndicator.stopAnimating()
                let alert = Constants()
                alert.showAlert(fromController: self, alertMessage: "Entered username or password is incorrect")
                break

            case .failure(let error):
                print("Request failed with error: \(error)")
            }
        }
    }
}

Please let me know what mistake am I doing and please let me know what changes do I need to do. 请让我知道我在做什么错误,并让我知道我需要做哪些更改。 I am not bale to display the alert message also. 我也不愿意显示警报消息。 I am getting the below warning: 我收到以下警告:

Warning: Attempt to present <UIAlertController: on whose view is not in the window hierarchy!

I think you just need to implement the following method, and it should work fine : 我认为您只需要实现以下方法,它就可以正常工作:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
   if (segue.identifier == "YOUR_SCENE_IDENTIFIER_TO_THE_NEXT_SCREEN") {
    // pass some data to the destination vc, if you need
   }

} }

执行序列的代码:

performSegue(withIdentifier: "loginSuccess", sender: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM