gpt4 book ai didi

objective-c - 在 Block 语句中分配/显示 UIAlertView

转载 作者:太空狗 更新时间:2023-10-30 03:34:36 27 4
gpt4 key购买 nike

我对 Objective-C 中的 block 还很陌生。我已经阅读了文档并且对它们有了非常基本的了解。

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

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

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

这使得在主线程中回调,对于混合 block 和 UIKit 很有用

关于objective-c - 在 Block 语句中分配/显示 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700411/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com