【问题标题】:Update UI after requestAccessToAccountsWithType在 requestAccessToAccountsWithType 之后更新 UI
【发布时间】:2013-05-07 02:59:22
【问题描述】:

我正在开发一个应用程序来帮助我理解 OBJECTIVE-X/OSX。

该应用程序只需连接到 Facebook 并使用 NSUserNotification 发送通知。 它工作正常,但现在我想添加一些 UI。

为了使示例更简单,我想更新一个标签 (NSTextField) 以显示 Facebook 连接的状态。

  1. 正在连接……

  2. 已连接

  3. 失败

我在一个文件中有以下代码FacebookRequest.m

- (void) connectFacebook{

    if(self.account == nil){
        self.account = [[ACAccountStore alloc]init];
    }

   ACAccountType *facebookAccount = [self.account 
            accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSDictionary *options = @{
                              ACFacebookAppIdKey: @"MY_CODE",
                              ACFacebookPermissionsKey: @[@"email", 
                                                         @"user_about_me",
                                                         @"user_likes",
                                                         @"manage_notifications",
                                                         @"user_activities"],
                              ACFacebookAudienceKey: ACFacebookAudienceFriends
                              };
    [self.account requestAccessToAccountsWithType:facebookAccount 
                                          options:options 
                                       completion:^(BOOL success, NSError *error){

        if(success){
            NSArray *accounts = [self.account accountsWithAccountType:facebookAccount];
            self.account = [accounts lastObject];
        }
        else{
            NSLog(@"Erro %@", [error description]);
        }

    }];

}

以及我的AppDelegate.m下面的一个

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self.statusFacebook setStringValue:@"Connecting…"];
    FacebookRequest *request = [[FacebookRequest alloc]init];
    [request connectFacebook];
}

在请求完成并且我有帐户后,更新 UI 的最佳方式是什么?

我遇到了麻烦,因为请求是异步的,我无法在 requestAccessToAccountsWithType 块内返回任何值。还有一点是,如果我在后面加上一些“ifs”来检查我的账户是否为nil,它会在块执行完之前执行,所以账户仍然是nil

谢谢!

PS.:如果英文不够清楚,请见谅。

【问题讨论】:

  • 为什么不能在 if(success) 中更新 UI??
  • 一旦你有了一个帐户,只需在主线程上执行一个选择器就可以了..
  • @Stats 谢谢,我会试试的。我遇到了麻烦,因为它们位于单独的文件/类中,我想我会将它们放在同一个文件中,这样对我来说会更容易=p
  • 您最好为此发布一个 nsnotification
  • 我相信这就是我想要的!非常感谢!然后我会看看 NSNotification

标签: facebook macos cocoa slrequest


【解决方案1】:

您可以为此目的使用NSNotificationCenter

[self.account requestAccessToAccountsWithType:facebookAccount 
                                          options:options 
                                       completion:^(BOOL success, NSError *error){

        if(success){
            NSArray *accounts = [self.account accountsWithAccountType:facebookAccount];
            self.account = [accounts lastObject];
            // You post a notification that the UI should update here
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateUI" object:nil];
        }
        else{
            NSLog(@"Erro %@", [error description]);
        }

    }];

然后,添加应更新其 UI 的 viewController 作为此通知的观察者:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI) name:@"UpdateUI" object:nil];

}
- (void)updateUI {
    // Here you actually update your UI
}

附言如果您不使用 arc,您还可以删除 dealloc 中的观察者:

 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self]; 

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2017-06-05
    • 1970-01-01
    • 2020-07-26
    • 2013-09-14
    相关资源
    最近更新 更多