使用NSArray填充UIActionSheet按钮

时间:2013-01-11 18:24:49

标签: iphone ios twitter uiactionsheet

我正在尝试提取UIActionSheet以显示当前已登录到Twitter设置的所有帐户。我得到所有帐户的NSArray匹配类型Twitter,然后运行此代码:

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秒。

1 个答案:

答案 0 :(得分:2)

你不能写[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]];
}