Swift3和Xcode8:执行函数后无法执行segue

时间:2016-11-02 11:03:22

标签: ios swift3 alamofire xcode8.1

我在检查输入的用户名和密码是否正确以及是否不正确之后尝试执行segue到主屏幕我正在尝试显示警告消息。我没有成功地执行其中任何一项。因为只要我点击登录按钮,就会显示一个白色屏幕。 以下是我的代码:

使用以下方法执行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
}

我的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)")
            }
        }
    }
}

请让我知道我在做什么错误,请让我知道我需要做哪些更改。我也不打算显示警报信息。 我收到以下警告:

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

2 个答案:

答案 0 :(得分:0)

我认为你只需要实现以下方法,它应该可以正常工作:

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
   }

}

答案 1 :(得分:0)

预先形成segue的代码:

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