【问题标题】:How do display a UIAlertView from a block on iOS?如何从 iOS 的块中显示 UIAlertView?
【发布时间】:2012-12-17 18:08:28
【问题描述】:

从块中显示 UIAlertView 的最佳方式是什么?

我的代码中有以下操作:

- (IBAction)connectWithTwitterClicked:(id)sender {
    ACAccountStore * account = [[ACAccountStore alloc]init];
    ACAccountType * accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if (granted == YES){
            NSLog(@"Twitter account has been granted");
            NSArray * arrayOfAccounts = [account accountsWithAccountType:accountType];
            if([arrayOfAccounts count] > 0){
                ACAccount * twitterAccount = [arrayOfAccounts lastObject];
                NSLog(@"Found twitter Id of [%@]", twitterAccount.username);
                // continue on to use the twitter Id
            } else {
                // no twitter accounts found, display alert to notify user
            }
        } else{
            NSLog(@"No twitter accounts have been granted");
            // no twitter accounts found, display alert to notify user
        }
    }];
}

到目前为止,我已经尝试过这些解决方案:

  1. 在 2 条注释行中的任何一条上,直接创建并显示 UIAlertView,这会使应用程序崩溃,我相信这是由于 该块是一个异步进程并且无法访问 用于显示警报的 UI 线程
  2. 在块外创建了一个 NSMutableString,用 __block,设置在注释行后显示。 类似的问题在这里块是异步运行的 显示警报的时间无法保证 NSMutableString 已设置。

谁能提出解决方案?我希望能够以某种方式通知用户,这样他们就可以不使用 twitter,或者在设备设置中设置一个帐户。

谢谢

【问题讨论】:

    标签: objective-c ios ios6 twitter objective-c-blocks


    【解决方案1】:

    这是执行此操作的 GCD 方式:

    [SomeClass dispatchNastyAsyncBlock:^{
        // ... do stuff, then
        dispatch_async(dispatch_get_main_queue(), ^ {
            [[[[UIAlertView alloc] initWithTitle:t
                                         message:nil
                                        delegate:nil
                               cancelButtonTitle:@"OK"
                               otherButtonTitles:nil
            ] autorelease] show];
        });
    }];
    

    【讨论】:

      【解决方案2】:

      创建一个显示警报视图的方法,然后在主线程上执行其选择器:

      - (void)showAlertWithTitle:(NSString *)t
      {
          [[[[UIAlertView alloc] initWithTitle:t
                                       message:nil
                                      delegate:nil
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil
          ] autorelease] show];
      }
      

      如下调用:

      [SomeClass dispatchNastyAsyncBlock:^{
          // ... do stuff, then
          [self performSelectorOnMainThread:@selector(showAlertWithTitle:)
                                 withObject:@"Here comes the title"
                              waitUntilDone:YES];
      }];
      

      【讨论】:

      • 如果您使用 ARC,请务必删除 autorelease 调用。
      猜你喜欢
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多