gpt4 book ai didi

更新 ACAccountStoreRequestAccessCompletionHandler for Twitter 中的按钮时出现 iOS 滞后

转载 作者:行者123 更新时间:2023-11-29 11:07:05 24 4
gpt4 key购买 nike

我正在尝试制作一个切换按钮来指示分享到 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];
}
}

感谢您提供的任何帮助。

最佳答案

我刚刚解决了一个类似的问题。这是因为 requestAccessToAccountsWithType 完成函数在不同的线程上,如果你想处理接口(interface),你必须回到主线程。

可以回到主线程:

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;
});
}
}];
}

关于更新 ACAccountStoreRequestAccessCompletionHandler for Twitter 中的按钮时出现 iOS 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com