【发布时间】:2015-04-21 14:32:19
【问题描述】:
我正在创建一个用户并在使用 Facebook SDK 后登录该用户。正确地将用户插入数据库中应该是这样。然后我想显示一个警报,在解除警报后,我想加载一个不同的故事板(基本上是主菜单)。
现在我的理解是错误是线程问题。但是..我将故事板等的加载作为一个块传递给警报。那么它不应该只在用户解除警报后才被调用吗?我还尝试让它在主线程上运行(提到了here),但我仍然收到错误消息。
我在这里错过了什么?
The originating call:
if DBUser.insertDictionary(user.getModelAsStringDictionary()) {
if DBUser.loginUserWithFacebookId(facebookId!){
self.accountCreatedAlert()
}else{
//log error, user could not be logged in.
}
}else{
// log error user account could not be inserted into the database
}
}
警报文本和switchToMainStoryboard()方法的异步捆绑:
private func accountCreatedAlert(fn:()){
let asyncMoveToMain = {dispatch_async(dispatch_get_main_queue()) {
self.switchToMainStoryboard()
}}
self.showAlert("You will now be logged in", title: "Account created", fn: asyncMoveToMain)
}
警报:
func showAlert(text : NSString, title : NSString, fn:()->Void){
var alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
UIApplication.sharedApplication().delegate?.window!?.rootViewController?.presentViewController(alert, animated: true, completion: fn)
}
故事板的加载位置:
func switchToMainStoryboard() ->Void{
let main = UIStoryboard(name: "Main", bundle: nil)
var viewController = main.instantiateInitialViewController() as UIViewController
viewController.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
self.presentViewController(viewController, animated: true, completion: nil)
}
【问题讨论】: