在Block语句中分配/显示UIAlertView

时间:2012-10-03 00:37:07

标签: objective-c objective-c-blocks

我对目标C中的障碍很陌生。我已经阅读了文档,我对它们有了非常基本的了解。

为什么这不起作用?这是一个用于请求日历访问的框架回调。它需要一个块作为参数。我想做的就是在块中分配并显示UIAlertView,但是当它试图显示时会崩溃。

我希望这不是一个愚蠢的问题......使用块的网上的所有介绍示例只显示计数器的简单示例。

//Request access
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {

            if (granted == FALSE) {
                UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied"
                                                                          message:@"<InfoText>"
                                                                         delegate:nil
                                                                cancelButtonTitle:@"OK"
                                                                otherButtonTitles:nil] autorelease];
                [myAlert show];
            }
            else {
                [self addToCalendar];
            }
        }];

1 个答案:

答案 0 :(得分:24)

你尝试过吗?

if (granted == FALSE)
{
    dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied"
                                                         message:@ <InfoText>"
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil] autorelease];
       [myAlert show];
    });
}

这使得在主线程中回调,对于混合块和UIKit

非常有用