我正在尝试制作一个切换按钮,以指示与Twitter分享,就像在拍照后出现的Instagram共享页面一样。具体来说,它是一个从Normal
状态开始的UIButton,然后当它被按下时,它进入Selected
状态,用户的Twitter句柄作为标题。
在第一次点击时,它会使用ACAccountStore检查存储的Twitter帐户。问题是requestAccessToAccountsWithType
函数中ACAccountStoreRequestAccessCompletionHandler的延迟。在处理程序中,我进行了更改以设置Twitter句柄并选择按钮。但是,按钮需要很长时间才能直观地更改所选状态。这是因为它在调用后由NSLog验证处于选定状态。
我无法弄清楚造成这种滞后的原因。这是处理按钮的代码。
- (IBAction)pressedTwitter:(id)sender {
UIButton *button = (UIButton*)sender;
if (button.selected) {
button.selected = !button.selected;
} else {
if (self.twitterAccount != nil) {
button.selected = !button.selected;
} else {
[self getTwitterAccount:button];
}
}
}
- (void)getTwitterAccount:(UIButton*)button
{
// Get Account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
self.twitterAccount = [twitterAccounts objectAtIndex:0];
[self.twitterButton setTitle:self.twitterAccount.username forState:UIControlStateSelected];
[self setTwitterButtonNormalText:YES];
self.twitterButton.selected = YES;
NSLog(@"Twitter Button Selected");
self.statusLabel.text = @"Twitter Account Found";
} else {
[self setTwitterButtonNormalText:NO];
button.selected = NO;
NSLog(@"Not enough Twitter Accounts");
}
} else {
NSLog(@"Twitter Account Permission Not Granted");
[self setTwitterButtonNormalText:NO];
button.selected = NO;
}
}];
}
- (void)setTwitterButtonNormalText:(BOOL)accountAvailable
{
if (accountAvailable) {
[self.twitterButton setTitle:@"Twitter" forState:UIControlStateNormal];
[self.twitterButton setTitleColor:[UIColor colorWithWhite:0.5 alpha:1.0] forState:UIControlStateNormal];
} else {
[self.twitterButton setTitle:@"No Twitter" forState:UIControlStateNormal];
[self.twitterButton setTitleColor:[UIColor colorWithRed:125./255. green:21./255. blue:0 alpha:1] forState:UIControlStateNormal];
}
}
感谢您提供的任何帮助。
答案 0 :(得分:2)
我刚解决了类似的问题。这是因为requestAccessToAccountsWithType完成函数位于不同的线程上,如果你想与接口反应,你必须返回主线程。
你可以回到主线程:
dispatch_sync(dispatch_get_main_queue(), ^{
// code that change the ui, perfom segues, ...
});
试试这个:
- (void)getTwitterAccount:(UIButton*)button
{
// Get Account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
self.twitterAccount = [twitterAccounts objectAtIndex:0];
// go back to the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
[self.twitterButton setTitle:self.twitterAccount.username forState:UIControlStateSelected];
[self setTwitterButtonNormalText:YES];
self.twitterButton.selected = YES;
});
NSLog(@"Twitter Button Selected");
self.statusLabel.text = @"Twitter Account Found";
} else {
// go back to the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
[self setTwitterButtonNormalText:NO];
button.selected = NO;
});
NSLog(@"Not enough Twitter Accounts");
}
} else {
NSLog(@"Twitter Account Permission Not Granted");
// go back to the main thread
dispatch_sync(dispatch_get_main_queue(), ^{
[self setTwitterButtonNormalText:NO];
button.selected = NO;
});
}
}];
}