iphone - 使用 NSArray 填充 UIActionSheet 按钮

标签 iphone ios twitter uiactionsheet

我正在尝试拉出 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];

但是,我收到错误“不允许分配给 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/

相关文章:

iPhone 5 视网膜显示屏 2x 图像

iphone - 请解释这个 Objective-C 代码

ios - 如何获取本地日历更新回调

javascript - 在 iPhone 上使用 Safari Mobile 打开弹出窗口中的 URL 时出现问题。忽略参数

iphone - 将 NSString 转换为 nsdate 不起作用

iphone - 将图像移动到随机占位符

iOS:如何使用Restkit设置上传到服务器的图像的名称

ios5 storyboard 版 Facebook 整合

javascript - 类型错误 : Cannot read property 'message' of undefined - Twitter API

python - Twython 连接超时。如何正确重启