UIActionSheet和Twitter用户名选择

bmu*_*ler 1 iphone twitter objective-c ios

我正在尝试为用户提供一种方法来从他们在iPhone上设置的Twitter帐户中选择(iOS5及更高版本).我可以让用户名出现在UIActionSheet中,但由于某种原因,UIActionSheet在调用方法后大约需要5秒钟.

我想也许是因为它需要一段时间来检索Twitter帐户列表,但在我的日志中它们会立即出现,所以不是这样.

有任何想法吗?

- (void)TwitterSwitch {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

// Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    NSMutableArray *buttonsArray = [NSMutableArray array];

// Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {


       // Get the list of Twitter accounts.
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

       NSLog(@"%@", accountsArray);


       [accountsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

           [buttonsArray addObject:((ACAccount*)obj).username];
        }];


       NSLog(@"%@", buttonsArray);

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for( NSString *title in buttonsArray)
            [actionSheet addButtonWithTitle:title];
        [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
        [actionSheet showFromTabBar:self.tabBarController.tabBar];
}]; 

}
Run Code Online (Sandbox Code Playgroud)

rde*_*mar 5

我认为问题可能在于requestAccessToAccountsWithType:withCompletionHandler:可以在任意队列上调用处理程序,并且您正在该处理程序中显示UI元素(操作表).由于与UI的交互只应在主线程上完成,这可能是一个问题.我尝试将一些代码移动到另一个方法,并在主线程上调用它 - 这要快得多:

- (void)TwitterSwitch {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];


    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {


        // Get the list of Twitter accounts.
        NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

        NSLog(@"%@", accountsArray);
        [self performSelectorOnMainThread:@selector(populateSheetAndShow:) withObject:accountsArray waitUntilDone:NO];
        }];
}

-(void)populateSheetAndShow:(NSArray *) accountsArray {
    NSMutableArray *buttonsArray = [NSMutableArray array];
    [accountsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [buttonsArray addObject:((ACAccount*)obj).username];
        }];


        NSLog(@"%@", buttonsArray);

        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
        for( NSString *title in buttonsArray)
            [actionSheet addButtonWithTitle:title];
        [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.cancelButtonIndex = actionSheet.numberOfButtons-1;
        [actionSheet showInView:self.view];
}
Run Code Online (Sandbox Code Playgroud)

请注意,我改变了显示工作表的方式,因为在我的测试应用程序中我没有标签栏控制器.