1

我正在尝试制作一个切换按钮,以指示共享到 Twitter,例如在拍照后出现的 Instagram 共享页面中。具体来说,它是一个以Normal状态开始的 UIButton,然后当它被按下时,它会进入Selected以用户的 Twitter 句柄为标题的状态。

在第一次点击时,它会使用 ACAccountStore 检查存储的 Twitter 帐户。问题是函数中 ACAccountStoreRequestAccessCompletionHandler 的滞后requestAccessToAccountsWithType性。在处理程序中,我进行了更改以设置 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];
    }
}

感谢您的任何帮助,您可以提供。

4

1 回答 1

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;
            });
        }
    }];
}
于 2013-02-20T00:38:29.943 回答