UIAlertView 每次调用都会弹出三次,而不是一次

2024-05-29

我在程序的两个不同部分中从 NSAlert 中得到了奇怪的行为。行为是:

  1. 出现警报,然后自发地消失。
  2. 警报重新出现,然后一直保留,直到用户解除,即正常行为。
  3. 警报再次出现。

此行为仅在第一次调用显示警报的方法时发生。第一次之后,它的行为就正常了。

以下是发生该行为的部分之一的代码:

UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [locationAlert show];
        [locationAlert release];

或者,如果您愿意,可以提供更多背景信息:

- (IBAction)locateMe {
NSLog(@"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation * )oldLocation {
if (newLocation.horizontalAccuracy >= 0) {

    CLLocation *airportLocation = [[[CLLocation alloc] initWithLatitude:51.500148 longitude:-0.204669] autorelease];
    CLLocationDistance delta = [airportLocation getDistanceFrom: newLocation];
    long miles = (delta * 0.000621371) + 0.5; //metres to rounded mile
    if (miles < 3) {
        UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [locationAlert show];
        [locationAlert release];
        [locMan stopUpdatingLocation];
    } else {
        UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are not in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [locationAlert show];
        [locationAlert release];
        [locMan stopUpdatingLocation];

    }
}
}

- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"Error." message:error.code delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[locationAlert show];
[locMan release];
locMan = nil;
}

有任何想法吗?谢谢。

编辑 - - - - -

发生这种情况的另一个地方是:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download feed from web site (Error code %i )", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);

UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

对于上下文,第一种情况是在 AppDelegate 中,第二种情况是在第一个选项卡视图的视图控制器中。当没有互联网连接时,每次重新加载 xml 时都会出现第二个问题。第一个仅在第一次调用该函数时发生。

编辑 - - -

如果我移动警报,它就会起作用。不幸的是这不是我想要的地方!

- (IBAction)locateMe {

 UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:@"You are in the right place." message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[locationAlert show];
/*
NSLog(@"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];*/
}

Update:

我设置了一些 NSLog 条目并发现尽管添加了[locMan stopUpdatingLocation]didUpdateToLocation 函数运行了多次。

我猜想自发消失的发生是因为再次调用警报视图并且程序自动清除第一个实例为第二个实例让路。

关于为什么的任何想法[locMan stopUpdatingLocation]不起作用将不胜感激,但同时我只是将 locationAlert 的声明移出函数(因此它是全局的),将其设置在初始的“定位我”函数中,并在第一次调用时使用以下内容:

[locationAlert show];
locationAlert = nil;

这样它就完美地工作了。


当您第一次显示提醒时,您并没有关闭位置管理器。随着设备对位置进行细化(即准确性提高),您的回调将(可能)被多次调用。您应该在警报显示后使用 [locMan stopUpdatingLocation]。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIAlertView 每次调用都会弹出三次,而不是一次 的相关文章

随机推荐

  • Traefik 2 网关超时

    所以我有以下 docker compose yml version 3 7 services roundclinic mysql image mysql 5 7 networks spring boot mysql network envi
  • SQL 查询结果为字符串(或变量)

    是否可以将SQL查询结果输出到一个字符串或变量中 我的php和mysql不好 假设我有数据库 agents 其中包含列 agent id agent fname agent lname agent dept 使用此查询 sql SELECT
  • 如何使用 pyinstaller 包含文件?

    我也使用 tkinter 使用 python 3 7 编写了一个程序 由于我使用的是外部图片 因此当我将所有内容编译为一个 exe 时 我需要包含它们 我试过做 add data bg png files 但我仍然收到此错误 tkinter
  • 如果输入无效,是否可以抛出异常?

    我有一个简单的 ANLTR 语法和随附的访客 一切都很好 除非输入无效 如果输入无效 错误就会被吞噬 并且我的计算器会输出错误的输出 我尝试过实现一个错误侦听器 超越Recover词法分析器的方法 还有 好吧 今天还有六件事 有人可以告诉我
  • 如何使用缓存快速重建dockerfile?

    我想优化我的 Dockerfile 我希望将缓存文件保留在磁盘中 但是 当我跑步的时候我发现docker build 它总是尝试从网络获取每个文件 我希望在构建期间共享我的缓存目录 例如 var cache yum x86 64 6 但是
  • 如何构建gcc multilib工具链?

    我正在尝试在新安装的 ubuntu 14 04 的 AMD64 版本上构建 gcc multilib 工具链 它只有 x86 64 gcc 和 g 安装 没有 multilib 支持 我的配置行是 configure disable che
  • 类型已知,但方法指的是缺失类型

    我对 java 和 Eclipse 不太有经验 但遇到以下问题 我正在写类似的东西 Point3D myPoint myClass myMethod arg 我收到错误 方法 myMethod myType arg 引用缺失的类型 Poin
  • 重用 PDO 语句 var 会使进程崩溃

    我重用一个变量来存储两个不同的 PDO mysql 语句 stmt dbh gt prepare SELECT stmt gt execute stmt dbh gt prepare UPDATE crash here Error in o
  • 优雅模板专业化

    是否有一种优雅的方法可以根据模板参数之一来专门化模板 Ie template
  • 如何在 Android 中的 Chrome 或 Firefox 等特定浏览器的 Web 视图中加载应用程序

    我是 Android 新手 我正在做一个应用程序 我需要在平板电脑上的 Web 视图中加载现有的应用程序 在平板电脑中 当我使用 Web 视图加载应用程序时 我的应用程序将加载到默认浏览器中 如何在平板电脑上的 Web 视图中的特定浏览器
  • 有没有办法在 C 中按多个变量对结构进行排序?

    我必须编写一个对数组中的结构进行排序的函数 结构是 define MAX USERNAME LENGTH 16 typedef struct char username MAX USERNAME LENGTH unsigned int ri
  • OSX 中的动态链接优先级之间存在冲突吗?

    OSX 上不同 libjpeg 动态库之间存在动态链接冲突 首先有一个标准的本机 libJPEG dylib 位于 System Library Frameworks ImageIO framework Versions A Resourc
  • 了解 C# 中的协变和逆变接口

    我在一本有关 C 的教科书中遇到过这些内容 但我很难理解它们 可能是由于缺乏上下文 对于它们是什么以及它们有什么用处 是否有一个很好的简洁解释 编辑以澄清 协变接口 interface IBibble
  • jQuery 日期格式

    如何使用 jQuery 设置日期格式 我正在使用下面的代码但出现错误 txtDate val format date new Date dd M yy 请提出解决方案 在您的页面中添加 jquery ui 插件 txtDate val da
  • CSS 属性选择器类以以下开头但不等于

    我想将 css 应用于以 abcd 开头的所有类 但我不想将该 css 应用于名称为 abcd dontapply 的类 我可以这样做吗 我尝试过的 a class abcd not class abcd dontapply define
  • 为什么梅森扭转器比线性同余发生器更快?

    我使用 gcc C 标准库的梅森扭曲器实现进行了测试 它的性能优于线性同余发生器和 Crand 这很可能是 LCG 提升文档 http www boost org doc libs 1 58 0 doc html boost random
  • 如何在 paper-jsdom-canvas 中使用自定义字体

    我正在使用 构建一个节点画布 2d 库paper jsdom canvas PaperJS 无法使用我正在注册的自定义字体registerFont的方法canvas 但是当我尝试使用canvas直接投影 字体渲染没有任何问题 PaperJS
  • 如何使用 linq 通过主键组合三个对象列表

    我正在尝试合并 3 个对象列表 我有一个人员列表 地址列表和地址关系列表 我想将这些列表组合成一个按 person id 排序的新列表 将其用作列表视图的数据源 然后能够访问 aspx 页面中的属性 这可能吗 Roughly using S
  • 是否可以仅突出显示图像的某些部分(不透明度)?

    我已对图像应用了不透明度 这是代码
  • UIAlertView 每次调用都会弹出三次,而不是一次

    我在程序的两个不同部分中从 NSAlert 中得到了奇怪的行为 行为是 出现警报 然后自发地消失 警报重新出现 然后一直保留 直到用户解除 即正常行为 警报再次出现 此行为仅在第一次调用显示警报的方法时发生 第一次之后 它的行为就正常了 以