Ios 尝试在已呈现UIAlertController的ViewController上呈现UIViewController
我刚刚开始使用swift iOS编程。我写了一些代码。很简单,我想执行一个警报,然后通过“performsguewithidentifier”移动到另一个视图控制器。但我得到了这个输出: “警告:尝试在Kilaundry上显示UIViewController:0x7fa05b72dd60。ViewController:0x7fa05b49a2c0已显示UIAlertController:0x7fa05d859d70” 我认为警告出现在以下代码之后:“NSOperationQueue.mainQueue().addOperationWithBlock” 为什么我不能执行警报,然后通过“PerformsgueWithIdentifier”移动到另一个视图控制器?请帮我找出为什么会发生这种警告 这是我的密码:Ios 尝试在已呈现UIAlertController的ViewController上呈现UIViewController,ios,xcode,swift,uiviewcontroller,uialertcontroller,Ios,Xcode,Swift,Uiviewcontroller,Uialertcontroller,我刚刚开始使用swift iOS编程。我写了一些代码。很简单,我想执行一个警报,然后通过“performsguewithidentifier”移动到另一个视图控制器。但我得到了这个输出: “警告:尝试在Kilaundry上显示UIViewController:0x7fa05b72dd60。ViewController:0x7fa05b49a2c0已显示UIAlertController:0x7fa05d859d70” 我认为警告出现在以下代码之后:“NSOperationQueue.mainQu
if let data = data, let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
print(jsonResult)
Resp_code = jsonResult["Resp_code"] as? String;
Resp_message = jsonResult["Resp_message"] as? String;
if Resp_code == "01" {
NSOperationQueue.mainQueue().addOperationWithBlock {
let alert = UIAlertController(title: "Information", message:Resp_message!, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
self.performSegueWithIdentifier("LoginSucceed", sender: self)
}
} else {
NSOperationQueue.mainQueue().addOperationWithBlock {
let alert = UIAlertController(title: "Oops!", message:"It seems "+Resp_message!, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
}
}
您收到此错误是因为您试图在已显示模态VC的视图控制器上显示模态视图控制器-您显示警报控制器,然后立即触发segue(可能也是模态)。 视图控制器在给定时间只能有一个显示视图控制器。做你想做的事情的最简单的方法是在VC上显示“LoginSucseed”segue显示的“OK”警报 但是,我会重新考虑你是否应该显示警报。我建议你读一读——基本上是说“除非你真的需要,否则不要显示警报”。特别是,不要显示通知用户应用程序正常运行的警报。
当您的请求失败时显示错误警报是正确的-当用户成功登录时显示错误警报不是必需的。您收到此错误是因为您试图在已显示模式VC的视图控制器上显示模式视图控制器-您显示警报控制器,然后立即触发一个segue(可能也是模态)。 视图控制器在给定时间只能有一个显示视图控制器。做你想做的事情的最简单的方法是在VC上显示“LoginSucseed”segue显示的“OK”警报 但是,我会重新考虑你是否应该显示警报。我建议你读一读——基本上是说“除非你真的需要,否则不要显示警报”。特别是,不要显示通知用户应用程序正常运行的警报。
当您的请求失败时显示错误警报是正确的-当用户成功登录时显示错误警报是不必要的。为什么要使用NSOperation queue?如果要在主线程上使用dipatch_get_main_queue(),我想您应该使用它/为什么要使用NSO操作队列?如果要在主线程上使用dipatch_get_main_queue(),我想您应该使用它/