2013-10-19 118 views
0

所以我试图使用ACAccountStore登录/注册用户。这发生在使用模态呈现的视图控制器上。它工作得很好,但是,当我关闭视图控制器时,底层/呈现视图控制器仍然是黑色窗口。我想这会发生,因为我不等待完成块完成。XCode - 块完成时执行代码

所以我的问题:如何在致电[self dismissViewControllerAnimated:YES completion:nil];之前等待完成块完成?

-(void)loginWithTwitter{ 

ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier: 
           ACAccountTypeIdentifierTwitter]; 

[account requestAccessToAccountsWithType:accountType options:nil 
           completion:^(BOOL granted, NSError *error) 
{ 
    if (granted) { 
     //do something -> call function to handle the data and dismiss the modal controller. 
    } 
    else{ 
     //fail and put our error message. 
    } 
}]; 
} 
+0

为什么你不能把它放在其他的?除非你需要某种延迟,否则为什么不延迟使用执行选择器。这不是很漂亮,但是C'est la vie。 –

回答

2

完成块是结束,将后(在此情况下帐户访问请求)来执行主处理该事情。所以你可以放入[self dismissViewControllerAnimated:YES completion:nil]

另一件事:由于保留周期,在块中引用self是不好的。你会修改你的代码,看起来像这样:

ACAccountStore *account = [[ACAccountStore alloc] init]; 
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier: 
     ACAccountTypeIdentifierTwitter]; 

__weak UIViewController *weakSelf = self; 
[account requestAccessToAccountsWithType:accountType options:nil 
           completion:^(BOOL granted, NSError *error) { 
    [weakSelf dismissViewControllerAnimated:YES completion:nil]; 

    if (granted) { 
     //do something -> call function to handle the data and dismiss the modal controller. 
    } 
    else { 
     //fail and put our error message. 
    } 

}]; 
+0

嗯。这个“有效” - 但并没有摆脱这个问题。这导致我相信在呈现视图控制器出现之前的简短“黑屏”与块没有任何关系。 – schnabler

+0

我认为底层视图控制器存在问题。你期望什么而不是黑屏? – LorikMalorik

+0

好 - 几乎是“常规”disMissViewControllerAnimated行为。 ModalView向下滚动,显示底层屏幕。但是,好像它实际上是底层控制器的问题。 – schnabler