Allocating/showing a UIAlertView in a Block statement

随声附和 提交于 2019-12-10 03:38:35

问题


I'm pretty new to blocks in objective C. I've read the docs and I have a pretty basic understanding of them.

Why won't this work? This is a framework callback for requesting Calendar access. It takes a block as an argument. All I want to do is allocate and show the UIAlertView in the block, but it will crash when it tries to show.

I hope this isn't a silly question... all the intro examples on the net using blocks just show trivial examples with counters.

//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:


have you tried?

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

this makes calls back in the main thread, useful for mixing blocks and UIKit



来源:https://stackoverflow.com/questions/12700411/allocating-showing-a-uialertview-in-a-block-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!