0

在检查输入的用户名和密码是否正确之后,我试图执行转到主屏幕,如果它们不正确,我试图显示一条警报消息。我在其中任何一个方面都没有成功。因为一旦我单击登录按钮,就会显示一个白屏。下面是我的代码:

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

2 回答 2

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
   }

}

于 2016-11-02T11:45:48.220 回答
0

执行segue的代码:

performSegue(withIdentifier: "loginSuccess", sender: nil)
于 2017-02-17T23:26:07.740 回答