【发布时间】:2016-10-20 16:38:34
【问题描述】:
我刚接触 swift iOS 编程。我写了一些代码。很简单,我想执行一个警报,然后通过“performSegueWithIdentifier”移动到另一个视图控制器。但我得到了这个输出:
“警告:尝试在 Kilaundry.ViewController: 0x7fa05b49a2c0 上呈现 UIViewController: 0x7fa05b72dd60 已经呈现 UIAlertController: 0x7fa05d859d70”
我认为警告在此代码之后:“NSOperationQueue.mainQueue().addOperationWithBlock”。
为什么我不能执行警报然后通过“performSegueWithIdentifier”移动到另一个视图控制器?请帮助我找出为什么会出现此警告。
这是我的代码:
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){}
}
}
}
【问题讨论】:
-
你为什么使用 NSOperation 队列?如果你想在主线程上使用它,我认为你想使用 dipatch_get_main_queue() 。 ://
标签: ios xcode swift uiviewcontroller uialertcontroller