UIAlertView每次呼叫弹出三次,而不是一次 [英] UIAlertView Pops Up Three Times per Call Instead of Just Once

查看:52
本文介绍了UIAlertView每次呼叫弹出三次,而不是一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting odd behavior from an NSAlert in two different parts of my program. The behavior is:

  1. 出现警报,然后自发消失.
  2. 再次出现警报,然后一直保留,直到被用户拒绝(即正常行为)为止.
  3. 警报再次出现.
  1. Alert appears and then spontaneously disappears.
  2. Alert reappears and then remains until dismissed by user i.e. normal behavior.
  3. Alert reappears again.

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

This behavior only occurs the first time the method that displays the alert is called. After that first time, it behaves normally.

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

Here is the code for the one of the parts in which the behavior occurs:

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

或者,如果您愿意,还可以提供一些上下文信息:

Or if you prefer, with a bit more context:

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

有什么想法吗?谢谢.

编辑---------

Edit---------

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

The other place this happens is:

- (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中,第二种情况在视图控制器中的第一个选项卡视图.当没有Internet连接时,每次重新加载xml时都会发生第二个问题.第一个仅在第一次调用该函数时发生.

For context the first case is in the AppDelegate and the second in the view controller for the 1st tab view. The second problem occurs every time the xml is reloaded when there is no internet connection. The first one only occurs the first time the function is called.

编辑-----

如果我移动警报,它将起作用.不幸的是,这不是我想要的!

If I move the alert it works. Unfortunatly this is not where I want it!

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

更新:

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

Update:

I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.

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

I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.

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

Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:

[locationAlert show];
locationAlert = nil;

那样,它可以完美地工作.

That way it works perfectly.

推荐答案

我认为NSAlert 消失本身就是解决这个问题的关键.

I think the NSAlert disappearing on its own is the key to solving this.

很容易解释为什么警报意外显示,即只是被意外调用.但是,以编程方式关闭警报并不常见.造成它消失的原因很可能再次触发了显示.

It's simple to explain why an alert displays unexpectedly i.e. it's just been called unexpectedly. However, it's not so common to programmatically dismiss an alert. Whatever is causing it to disappear is most likely triggering the display again.

要调试,我建议:

(1)在代码中查找NSAlert – dismissWithClickedButtonIndex:animated:方法,并查看您是否实际上以编程方式消除了警报.

(1) Looking in your code for the NSAlert – dismissWithClickedButtonIndex:animated: method and see if somehow you're actually dismissing the alert programmatically.

(2)我相信(对此进行了仔细检查的人),警报视图将作为子视图添加到当前屏幕上的任何基本视图中.可能是由于某种原因基本视图消失了,并带走了警报视图.如果视图消失,然后足够迅速地重新出现,则当​​警报位于最前面时,它可能并不明显. (请参见下面的Ed Marty的评论.)

(2) I believe (someone double-check me on this) that an alert view is added as a subview to whichever base view is currently on screen. It might be that the base view is disappearing for some reason and taking the alert view with it. If the view disappears and then reappears rapidly enough, it might not be obvious when the alert is frontmost. ( see Ed Marty's comment below.)

(3)由于这发生在应用程序的两个单独的部分中,因此请比较两者以找到共同的元素或结构.该共同因素可能是原因. 一个奇怪的问题.

(3) Since this happens in two separate pieces of the app, compare both to find a common element or structure. That common element might be the cause. An odd problem.

如果locMan是实例变量,则应将其定义为属性,并且每次使用self.locMan对其进行访问.通过直接访问它,您将失去自动保留管理功能.

If locMan isan instance variable, it should be defined as a property and you should access it every time withself.locMan By accessing it directly, you lose your automatic retention management.

这篇关于UIAlertView每次呼叫弹出三次,而不是一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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