gpt4 book ai didi

iphone - 使用 NSArray 填充 UIActionSheet 按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:42 25 4
gpt4 key购买 nike

我试图拉出一个 UIActionSheet 来显示当前登录到 Twitter 设置的所有帐户。我获取了所有匹配类型 Twitter 的帐户的 NSArray,然后运行此代码:

UIActionSheet *chooseaccount = [[UIActionSheet alloc]initWithTitle:@"Choose Account" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
for (int i = 0; i < [arrayOfAccounts count]--; i++) {
[chooseaccount addButtonWithTitle:[arrayOfAccounts objectAtIndex:i]];
}
[chooseaccount addButtonWithTitle:@"Cancel"];
chooseaccount.cancelButtonIndex = arrayOfAccounts.count;
chooseaccount showInView:self.tabBarController.view];

但是,我收到错误消息“不允许分配给‘readonly’返回的 objective-c 消息结果”

对于让 UIActionSheet 显示 Twitter 帐户的所有用户名有什么建议吗?

更新:
经过几条建议后,我将代码更改为:

-(void)twitteraccess {
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];

[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSLog(@"Granted");

NSArray *arrayOfAccounts = [account
accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
NSLog(@"%i", [arrayOfAccounts count]);

if ([arrayOfAccounts count] > 1) {
NSLog(@"Greaterthan1");
UIActionSheet *chooseaccount = [[UIActionSheet alloc]initWithTitle:@"Choose Account" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
for (int i = 0; i < [arrayOfAccounts count]; i++) {

ACAccount * account = [arrayOfAccounts objectAtIndex:i];
[chooseaccount addButtonWithTitle:account.username];
NSLog(@"%@", account.username);
}

[chooseaccount addButtonWithTitle:@"Cancel"];
chooseaccount.cancelButtonIndex = arrayOfAccounts.count;

[chooseaccount showInView:self.tabBarController.view];
}
else {

}

}
}
else {
if(error.code == 6){
[self performSelectorOnMainThread:@selector(alertmessagetwitter)
withObject:nil
waitUntilDone:YES]; }
}
}];
}

操作表确实会出现,但需要大约 25 秒才能出现。

最佳答案

你不能写[arrayOfAccounts count]--,这实际上扩展为:

[arrayOfAccounts count] = [arrayOfAccounts count] - 1; // assigning to readonly

相反,只需使用 [arrayOfAccounts count] - 1(或快速枚举)。

for (int i = 0; i < [arrayOfAccounts count]-1; i++) { //...


关于 InvalidArgumentException 的编辑:

假设 arrayOfAccounts 包含 ACAccount 对象而不是 NSString:

for (int i = 0; i < [arrayOfAccounts count]-1; i++) { //...
ACAccount * account = [arrayOfAccounts objectAtIndex:i];
[chooseaccount addButtonWithTitle:[account userName]];
}

关于iphone - 使用 NSArray 填充 UIActionSheet 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14284368/

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