在Block语句中分配/显示UIAlertView [英] Allocating/showing a UIAlertView in a Block statement

查看:75
本文介绍了在Block语句中分配/显示UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对目标C中的代码块还很陌生.我已经阅读了文档,并对它们有相当基本的了解.

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

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

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

推荐答案

您尝试过吗?

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很有用

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

这篇关于在Block语句中分配/显示UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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