Swift 尝试呈现其视图不在 window 层次结构中的 UIAlertController(在 TWTRShareEmailViewController 之后呈现)
Swift Attempt to present UIAlertController whose view is not in the window hierarchy (presented after TWTRShareEmailViewController)
我在应用程序的注册过程中使用 Twitter 登录。我正在询问用户的电子邮件。一旦我得到它,我想展示一个 UIAlertController。
这是我的代码:
func askForTWMail(){
if (Twitter.sharedInstance().session() != nil) {
let shareMailVC=TWTRShareEmailViewController(completion: {(mail:String!, error:NSError!) in
if (mail != nil) {
print("GOT MAIL: \(mail)")
self.gotMail()
}else{
print("MAIL VC ERROR: \(error)")
}
})
println("PRESENT MAIL VC")
self.presentViewController(shareMailVC, animated: true, completion: nil)
}else{
println("User not logged in")
}
}
func gotMail(){
var alertController=UIAlertController(title: "Some title", message: "Some message", preferredStyle: UIAlertControllerStyle.Alert)
var okAction=UIAlertAction(title:"Yes", style: UIAlertActionStyle.Default) {
UIAlertAction in
//some action
}
var cancelAction=UIAlertAction(title:"No", style: UIAlertActionStyle.Cancel){
UIAlertAction in
//some action
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
但是我得到这个错误(我猜是因为 TWTRShareEmailViewController 没有被关闭):
Warning: Attempt to present UIALertController on xViewController whose
view is not in the window hierarchy!
知道我应该如何写这篇文章吗?我如何知道 TWTRShareEmailViewController
何时被关闭以继续注册过程并能够出示我的 UIAlertController
?我不知道与 TWTRShareEmailViewController
.
相关的委托方法
感谢任何帮助。谢谢。
找到解决方案here。我可能做错了,但如果不是,那可能是 Apple 的错误。解决方法是延迟显示 UIAlertController
:
dispatch_async(dispatch_get_main_queue(), ^{
self.presentViewController(alertController, animated: true, completion: nil)
})
编辑:我找到了另一种解决方法(我不再使用我在这里提出的解决方案)。我必须更改此设置,因为 Twitter 登录也中断了我在 VC 秒之间的转换。
我现在调用一个特定的 UIViewController
(我称之为 TWLoginVC
),我在其中进行所有 Twitter 登录和其他操作。该视图是黑色的,因此用户看不到该过程实际上是在另一个 VC 中完成的(他只需要选择他想要登录的 Twitter 用户)。我想你也可以放一个清晰的背景来更加不可见。
当我调用这个视图控制器并关闭它时,过渡没有应用到它,我不再有任何问题。
编辑
Swift 更新:
DispatchQueue.main.async{
self.present(alertController, animated: true, completion: nil)
}
这是根据 Marie Dm 的回答在 Xcode 8 上测试的 Swift 3 的更新答案。
DispatchQueue.main.sync {
self.present(alertController, animated: true, completion: nil)
}
如果 DispatchQueue.main.sync
造成挤压,请尝试 DispatchQueue.main.async
。
"async" 从 contactPicker()
返回时解决了我的问题。
延迟呈现 viewController 解决了我的问题并挽救了我的一天 :D
Swift 4.0 :
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
//present your view controller her
self.presentViewController(alertController, animated: true, completion: nil)
}
我在应用程序的注册过程中使用 Twitter 登录。我正在询问用户的电子邮件。一旦我得到它,我想展示一个 UIAlertController。
这是我的代码:
func askForTWMail(){
if (Twitter.sharedInstance().session() != nil) {
let shareMailVC=TWTRShareEmailViewController(completion: {(mail:String!, error:NSError!) in
if (mail != nil) {
print("GOT MAIL: \(mail)")
self.gotMail()
}else{
print("MAIL VC ERROR: \(error)")
}
})
println("PRESENT MAIL VC")
self.presentViewController(shareMailVC, animated: true, completion: nil)
}else{
println("User not logged in")
}
}
func gotMail(){
var alertController=UIAlertController(title: "Some title", message: "Some message", preferredStyle: UIAlertControllerStyle.Alert)
var okAction=UIAlertAction(title:"Yes", style: UIAlertActionStyle.Default) {
UIAlertAction in
//some action
}
var cancelAction=UIAlertAction(title:"No", style: UIAlertActionStyle.Cancel){
UIAlertAction in
//some action
}
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
但是我得到这个错误(我猜是因为 TWTRShareEmailViewController 没有被关闭):
Warning: Attempt to present UIALertController on xViewController whose view is not in the window hierarchy!
知道我应该如何写这篇文章吗?我如何知道 TWTRShareEmailViewController
何时被关闭以继续注册过程并能够出示我的 UIAlertController
?我不知道与 TWTRShareEmailViewController
.
感谢任何帮助。谢谢。
找到解决方案here。我可能做错了,但如果不是,那可能是 Apple 的错误。解决方法是延迟显示 UIAlertController
:
dispatch_async(dispatch_get_main_queue(), ^{
self.presentViewController(alertController, animated: true, completion: nil)
})
编辑:我找到了另一种解决方法(我不再使用我在这里提出的解决方案)。我必须更改此设置,因为 Twitter 登录也中断了我在 VC 秒之间的转换。
我现在调用一个特定的 UIViewController
(我称之为 TWLoginVC
),我在其中进行所有 Twitter 登录和其他操作。该视图是黑色的,因此用户看不到该过程实际上是在另一个 VC 中完成的(他只需要选择他想要登录的 Twitter 用户)。我想你也可以放一个清晰的背景来更加不可见。
当我调用这个视图控制器并关闭它时,过渡没有应用到它,我不再有任何问题。
编辑 Swift 更新:
DispatchQueue.main.async{
self.present(alertController, animated: true, completion: nil)
}
这是根据 Marie Dm 的回答在 Xcode 8 上测试的 Swift 3 的更新答案。
DispatchQueue.main.sync {
self.present(alertController, animated: true, completion: nil)
}
如果 DispatchQueue.main.sync
造成挤压,请尝试 DispatchQueue.main.async
。
"async" 从 contactPicker()
返回时解决了我的问题。
延迟呈现 viewController 解决了我的问题并挽救了我的一天 :D
Swift 4.0 :
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
//present your view controller her
self.presentViewController(alertController, animated: true, completion: nil)
}