Swift尝试呈现其视图不在窗口层次结构中的UIAlertController(在TWTRShareEmailViewController之后呈现) [英] Swift Attempt to present UIAlertController whose view is not in the window hierarchy (presented after TWTRShareEmailViewController)

查看:277
本文介绍了Swift尝试呈现其视图不在窗口层次结构中的UIAlertController(在TWTRShareEmailViewController之后呈现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序的注册过程中使用Twitter登录。我要求用户的电子邮件。一旦得到它,我想提出一个UIAlertController。

I'm using the Twitter login in the signup process of my app. And I'm asking for the user's email. Once I get it, I'd like to present a 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不被忽略):

But I get this error (I guess because the TWTRShareEmailViewController is not dismissed):


警告:尝试在xViewController上呈现UIALertController,其
视图不在窗口层次结构中!

Warning: Attempt to present UIALertController on xViewController whose view is not in the window hierarchy!

我应该写这个吗如何知道 TWTRShareEmailViewController 何时被关闭以继续注册过程,并能够呈现我的 UIAlertController ?我不知道与 TWTRShareEmailViewController 相关的委托方法。

Any idea of how I should write this? How can I know when the TWTRShareEmailViewController is dismissed to continue the signup process and be able to present my UIAlertController? I'm not aware of a delegate method related to TWTRShareEmailViewController.

任何帮助都不胜感激。谢谢。

Any help is appreciated. Thanks.

推荐答案

找到解决方案此处。我可能做错了,但如果不是这可能是一个苹果的bug。解决方法是延迟 UIAlertController 的演示:

Found a solution here. I'm probably doing it wrong but if not it might be an Apple bug. The workaround is to delay the presentation of the UIAlertController:

dispatch_async(dispatch_get_main_queue(), {
    self.presentViewController(alertController, animated: true, completion: nil)
})

编辑:我发现另一种解决方法(我不再使用我放在这里的解决方案了)。我不得不改变这一点,因为Twitter的登录也破坏了我的VC之间的转换。

EDIT: I found another workaround (I don't use the solution I put down here anymore). I had to change this because the Twitter login was also breaking my transitions between VCs.

我现在调用一个特定的 UIViewController TWLoginVC )在哪里我做所有的Twitter登录和其他东西。视图只是黑色,所以用户看不到该进程实际上是在另一个VC中完成的(他只需要拿起他想要登录的Twitter用户)。我想你也可以把一个明确的背景变得更加隐形。

I now call a specific UIViewController (I called it something like TWLoginVC) where I do all the Twitter login and other stuff. The view is just black so the user don't see the process is actually done in another VC (he just has to pick up the Twitter user he wants to login with). I guess you could also put a clear background to be even more invisible.

当我打电话给这个视图控制器并关闭它时,转换不适用于它,我没有更多的问题。

When I call this view controller and dismiss it, the transition is not applied to it and I don't have any more problem with it.

编辑
Swift更新:

EDIT Update for Swift:

DispatchQueue.main.async{
     self.present(alertController, animated: true, completion: nil)
}

这篇关于Swift尝试呈现其视图不在窗口层次结构中的UIAlertController(在TWTRShareEmailViewController之后呈现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆