【问题标题】:Why is this UIViewController not updating after a call to twitter?为什么这个 UIViewController 在调用 twitter 后没有更新?
【发布时间】:2014-04-08 21:33:06
【问题描述】:

我正在尝试构建一个基本的 twitter 客户端作为学习 iOS 开发的练习。到目前为止,我有一个 TabBarController,其中一个选项卡是用户的时间线(工作正常),而这个新选项卡用于用户的个人资料。 twitter 调用下面的大部分结构来自一个在线教程,使用 XCode 5 和 twitter API 1.1 获取用户的时间线。然后我对其进行了修改以在第二个 ViewController 中获取用户的个人资料。在此个人资料视图中,我有一个 UIImageView 用于用户的个人资料图片,并有一个标签表示关注者的数量。

当我第一次点击此选项卡以查看个人资料视图时,imageView 为空白。当我第二次单击另一个选项卡,然后单击此配置文件选项卡时,将加载配置文件图像。关注者标签永远不会更新,但是当我调试它时,我可以看到我正确获取了 numFollowers 值。

那么为什么profileImageView在视图的初始视图上没有更新,为什么numFollowers标签从来没有更新呢?

ViewController的相关位:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self twitterProfile];
}

-(void) twitterProfile {
    ACAccountStore *account = [[ACAccountStore alloc] init];

    // Asks for the Twitter accounts configured on the device

    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
     {
         // if we have access to the Twitter accounts configured on the device we will contact the Twitter API

         if (granted) {
             // Retrieve array of twitter accounts on device
             NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
             // if there is at least one account we will contact the Twitter API
             if ([arrayOfAccounts count] > 0) {
                 ACAccount *twitterAccount = [arrayOfAccounts lastObject];
                 NSString *username = twitterAccount.username;
                 // API call that returns a user's profile
                 NSURL *requestAPI = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json" ];
                 // this is where we are getting the data using SLRequest
                 SLRequest *profile = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:requestAPI parameters:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%@", username], @"screen_name", @"-1", @"cursor", nil]];
                 profile.account = twitterAccount;
                 // the postRequest: method call now accesses the NSData object returned
                 [profile performRequestWithHandler:^(NSData *response, NSHTTPURLResponse *urlResponse, NSError *error) {

                     NSDictionary *profileData = [NSJSONSerialization JSONObjectWithData:response
                                                                  options:NSJSONReadingMutableContainers
                                                                    error:&error];
                     if (profileData.count > 0) {
                         NSString *profileImageURL = [profileData objectForKey:@"profile_image_url"];
                         NSURL *imageUrl = [[NSURL alloc] initWithString:profileImageURL];
                         UIImage *profileImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
                         _profileImageView.image = profileImage;
                         NSString *numFollowers = [profileData objectForKey:@"followers_count"];
                         _numFollowers.text = numFollowers;
                     }
                 }];
             }
         } else {
             // Handle failure to get account access
             NSLog(@"%@", [error localizedDescription]);
         }
     }];
}

【问题讨论】:

    标签: ios twitter


    【解决方案1】:

    您应该使用以下代码在前台执行更新:

    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *profileImageURL = [profileData objectForKey:@"profile_image_url"];
        NSURL *imageUrl = [[NSURL alloc] initWithString:profileImageURL];
        UIImage *profileImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
        _profileImageView.image = profileImage;
        NSString *numFollowers = [profileData objectForKey:@"followers_count"];
        _numFollowers.text = numFollowers;
    });
    

    【讨论】:

    • 谢谢,解决了;那个 dispatch_async 在 twitter 调用的原始处理中,当我为这个调用复制和修改它时,我已经删除了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多