如何在iOS上的块中显示UIAlertView? [英] How do display a UIAlertView from a block on iOS?

查看:159
本文介绍了如何在iOS上的块中显示UIAlertView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the best way of displaying a UIAlertView from a block?

我在代码中执行以下操作:

I have the following action in my code :

- (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
        }
    }];
}

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

I've tried these solutions so far :

  1. 在2条评论行中的任意一条上,直接创建并显示一个 UIAlertView,这会使应用程序崩溃,我相信这是由于 该块是异步过程,无法访问 用于显示警报的UI线程
  2. 在块外创建了一个NSMutableString,并标记为 __block,将其设置在注释行上,然后显示. 这里出现类似的问题,即该块异步运行,因此在 显示警报的时间无法保证 NSMutableString已设置.
  1. On either of the 2 commented lines, directly create and show a UIAlertView, this crashes the application, I believe this is due to the block being an asynchronous process and not having access to the UI thread to display the alert
  2. Created an NSMutableString outside the block, marked it with __block, set it on the commented lines and then displayed after. Similar problem here whereby the block is run asynchronously so at the time of displaying the alert theres no guarantee the NSMutableString has been set.

有人可以提出解决方案吗?我希望能够以某种方式通知用户,以便他们要么不使用Twitter,要么在设备设置中关闭并设置帐户.

Can anyone suggest a solution? I want to be able to notify the user somehow so they can either not use twitter, or to go off and setup an account in the device settings.

谢谢

推荐答案

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

Create a method that shows the alert view, then perform its selector on the main thread:

- (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];
}];

这篇关于如何在iOS上的块中显示UIAlertView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆