在Block语句中分配/显示UIAlertView

Sha*_*ram 7 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];
            }
        }];
Run Code Online (Sandbox Code Playgroud)

Yul*_*ega 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];
    });
}
Run Code Online (Sandbox Code Playgroud)

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