gpt4 book ai didi

ios - 以编程方式转换 View Controller 时出错

转载 作者:行者123 更新时间:2023-11-28 08:52:37 24 4
gpt4 key购买 nike

我正在使用此代码在用户登录后转换 View Controller :

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

当我在编辑器中时,代码不会引发任何错误,但是当它运行时什么也没有发生,我返回此错误:

2015-11-19 12:06:08.835 collaboration[8788:70890] Warning: Attempt to present on which is already presenting

View Controller “messagesViewController”存在,它的 Storyboard场景具有标识符“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")
}
}

最佳答案

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

您正在调用 self.sendAlert,我假设它会在显示您的 messagesVC Controller 之前在当前 View Controller 上显示一个 UIAlertController。这是在主线程上连续发生的,看起来您在显示警报时正在显示消息 Controller 。为什么不等到消息 View Controller 加载后才显示警报,或者将 View Controller 作为警报 Controller 上的 UIAlertAction 呈现?

其次,您正在调用函数 self.loginStatus()。如果您在该函数中显示警报或任何其他 View Controller ,那也可能是您的问题。

关于ios - 以编程方式转换 View Controller 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33810211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com