简体   繁体   中英

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. 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:

@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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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