以编程方式转换视图控制器时出错

时间:2015-11-19 17:10:52

标签: ios xcode swift


我在用户登录后使用此代码转换视图控制器:

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("messagesVC") as! messagesViewController
self.presentViewController(controller, animated: true, completion: nil)

当我在编辑器中时,代码不会引发任何错误,但是当它运行时没有任何反应并且我返回时出现此错误:

  

2015-11-19 12:06:08.835合作[8788:70890]警告:尝试   在场    已经是   提出

视图控制器'messagesViewController'存在,它的故事板场景具有标识符'messagesVC'。我对错误感到难过......如果你知道请帮助我 谢谢

登录按钮:

@IBAction func loginButton(sender: UIButton) {
        let savedUsername = NSUserDefaults.standardUserDefaults().stringForKey("username")! as String
        if savedUsername == "" {
        if usernameInput.text != "" && passwordInput.text != "" {
            let usernameTextFieldString = usernameInput.text! as String
            let passwordTextFieldString = passwordInput.text! as String
            let myUrl = NSURL(string: "http://127.0.0.1/bubble/login.php?username=\(usernameTextFieldString)&password=\(passwordTextFieldString)&secretKey=kjdfb4u7bksSDDF44wlksdnw33j4")
            let request = NSMutableURLRequest(URL: myUrl!)
            request.HTTPMethod = "POST"
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
                data, response, error in
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
                    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
                    if error != nil {
                        print("Error: \(error)")
                    }
                    dispatch_async(dispatch_get_main_queue()) {
                        let userInfo = responseString!.componentsSeparatedByString("9254203598")
                        let userInfoCount = userInfo.count
                        if userInfoCount == 4 {
                            let userInfoId = userInfo[1] as String
                            let userInfoUsername = userInfo[2] as String
                            let userInfoPassword = userInfo[3] as String
                            NSUserDefaults.standardUserDefaults().setObject(userInfoId, forKey: "id")
                            NSUserDefaults.standardUserDefaults().setObject(userInfoUsername, forKey: "username")
                            NSUserDefaults.standardUserDefaults().setObject(userInfoPassword, forKey: "password")
                            NSUserDefaults.standardUserDefaults().synchronize()
                            let keyId = NSUserDefaults.standardUserDefaults().stringForKey("id")
                            let keyUsername = NSUserDefaults.standardUserDefaults().stringForKey("username")
                            let keyPassword = NSUserDefaults.standardUserDefaults().stringForKey("password")
                            if keyId != nil && keyUsername != nil && keyPassword != nil {
                                self.sendAlert("Success", message: "You have been logged in successfully")
                                self.usernameInput.text = ""
                                self.passwordInput.text = ""
                                self.loginStatus()
                    //VIEW CONTROLLER TRANSITION TO FIX
                                let controller = self.storyboard?.instantiateViewControllerWithIdentifier("messagesVC") as! messagesViewController
                                self.presentViewController(controller, animated: true, completion: nil)
                            }
                        } else {
                            self.sendAlert("Error", message: "Unable to login")
                        }
                    }
                }
            }
            task.resume()
            loginStatus()
        } else {
            sendAlert("Login Invalid", message: "The username and password you entered are not associated with an account")
        }
        } else {
            self.sendAlert("Error", message: "You must be logged in to log out")
        }
    }

修订代码:

@IBAction func loginButton(sender: UIButton) {
        let savedUsername = NSUserDefaults.standardUserDefaults().stringForKey("username")! as String
        if savedUsername == "" {
        if usernameInput.text != "" && passwordInput.text != "" {
            let usernameTextFieldString = usernameInput.text! as String
            let passwordTextFieldString = passwordInput.text! as String
            let myUrl = NSURL(string: "http://127.0.0.1/bubble/login.php?username=\(usernameTextFieldString)&password=\(passwordTextFieldString)&secretKey=kjdfb4u7bksSDDF44wlksdnw33j4")
            let request = NSMutableURLRequest(URL: myUrl!)
            request.HTTPMethod = "POST"
            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
                data, response, error in
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
                    let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
                    if error != nil {
                        print("Error: \(error)")
                    }
                    dispatch_async(dispatch_get_main_queue()) {
                        print(responseString)
                        let userInfo = responseString!.componentsSeparatedByString("9254203598")
                        let userInfoCount = userInfo.count
                        if userInfoCount == 4 {
                            let userInfoId = userInfo[1] as String
                            let userInfoUsername = userInfo[2] as String
                            let userInfoPassword = userInfo[3] as String
                            NSUserDefaults.standardUserDefaults().setObject(userInfoId, forKey: "id")
                            NSUserDefaults.standardUserDefaults().setObject(userInfoUsername, forKey: "username")
                            NSUserDefaults.standardUserDefaults().setObject(userInfoPassword, forKey: "password")
                            NSUserDefaults.standardUserDefaults().synchronize()
                            let keyId = NSUserDefaults.standardUserDefaults().stringForKey("id")
                            let keyUsername = NSUserDefaults.standardUserDefaults().stringForKey("username")
                            let keyPassword = NSUserDefaults.standardUserDefaults().stringForKey("password")
                            if keyId != nil && keyUsername != nil && keyPassword != nil {
                                self.sendAlert("Success", message: "You have been logged in successfully")
                                self.usernameInput.text = ""
                                self.passwordInput.text = ""
                                NSUserDefaults.standardUserDefaults().stringForKey("username")
                                //self.loginStatus()
                                //VIEW CONTROLLER TRANSITION TO FIX
                                let controller = self.storyboard?.instantiateViewControllerWithIdentifier("messagesVC") as! messagesViewController
                                self.presentViewController(controller, animated: true, completion: nil)
                            }
                        } else {
                            print("Unable to login")
                            //self.sendAlert("Error", message: "Unable to login")
                        }
                    }
                }
            }
            task.resume()
            //loginStatus()
        } else {
            //sendAlert("Login Invalid", message: "The username and password you entered are not associated with an account")
        }
        } else {
            //sendAlert("Error", message: "You must be logged in to log out")
        }
    }

1 个答案:

答案 0 :(得分:0)

我看到两件事可能是罪魁祸首:

您正在调用self.sendAlert,我假设在展示您的UIAlertController控制器之前,它会在当前视图控制器上显示messagesVC。这是在主线程上串行发生的,看起来你在呈现警报时会显示消息控制器。为什么不等到消息视图控制器加载之后才显示警报,或者在警报控制器上将视图控制器显示为UIAlertAction

其次,您正在调用函数self.loginStatus()。如果您在该功能中提供警报或任何其他视图控制器,那也可能是您的问题。