我在应用程序的注册过程中使用 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 没有被忽略):
警告:尝试在 xViewController 上呈现 UIAlertController,其
视图不在窗口层次结构中!
知道我应该如何写这个吗?我怎么知道什么时候TWTRShareEmailViewController
被解雇以继续注册过程并能够展示我的UIAlertController
?我不知道与以下相关的委托方法TWTRShareEmailViewController
.
任何帮助表示赞赏。谢谢。
找到了解决方案here https://stackoverflow.com/questions/24854802/presenting-a-view-controller-modally-from-an-action-sheets-delegate-in-ios8。我可能做错了,但如果不是的话,这可能是苹果的一个错误。解决方法是延迟显示UIAlertController
:
dispatch_async(dispatch_get_main_queue(), ^{
self.presentViewController(alertController, animated: true, completion: nil)
})
EDIT:我找到了另一个解决方法(我不再使用我在这里放置的解决方案)。我必须改变这一点,因为 Twitter 登录也破坏了我在 VC 之间的转换。
我现在调用一个特定的UIViewController
(我称之为类似TWLoginVC
)我在那里进行所有 Twitter 登录和其他操作。该视图只是黑色的,因此用户看不到该过程实际上是在另一个 VC 中完成的(他只需选择他想要登录的 Twitter 用户)。我想你也可以设置一个清晰的背景,使其更加隐形。
当我调用此视图控制器并关闭它时,转换不会应用于它,并且我不再有任何问题。
EDIT斯威夫特更新:
DispatchQueue.main.async{
self.present(alertController, animated: true, completion: nil)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)