所以我正在尝试使用 ACAccountStore 登录/注册用户。这是使用模态呈现的 View Controller 发生的。它以这种方式工作得很好,但是,当我关闭 View Controller 时,底层/呈现 View Controller 仍然是一个黑色窗口。我假设会发生这种情况,因为我没有等待完成 block 完成。
所以我的问题是:如何在调用 [self dismissViewControllerAnimated:YES completion:nil];
之前等待完成 block 完成?
-(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.
}
}];
}
最佳答案
完成 block 是将在主进程(在本例中为访问帐户请求)完成后执行的事情。所以你可以把 [self dismissViewControllerAnimated:YES completion:nil]
放在里面。
另一件事:由于保留循环,在 block 中引用 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.
}
}];
关于ios - XCode - block 完成时执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19468354/