Ios UIAlertView显示两次

Ios UIAlertView显示两次,ios,iphone,uialertview,Ios,Iphone,Uialertview,希望你们都做得很好 我的问题是一个UIAlertView在执行代码时显示两次。我在-(void)viewdide出现:(BOOL)动画中编写了它,如下所示 -(void)viewDidAppear:(BOOL)animated { //Finding city Name from coordinate UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:@"Coordinate" message:[NSSt

希望你们都做得很好

我的问题是一个UIAlertView在执行代码时显示两次。我在
-(void)viewdide出现:(BOOL)动画中编写了它,如下所示

-(void)viewDidAppear:(BOOL)animated
{
//Finding city Name from coordinate
    UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:@"Coordinate" message:[NSString stringWithFormat:@"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];        
    [alertForCoordinate show];

    [SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(self.userLocation.coordinate.latitude, self.userLocation.coordinate.longitude)
     completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error)
    {

        if (placemarks.count>0)
        {

            CLPlacemark * placemark=[placemarks objectAtIndex:0];
            currentLocationUpcoming =[placemark locality];

            UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Location" message:[NSString stringWithFormat:@"currentLocation==>%@",currentLocationUpcoming] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];

            [self.eventsTableView reloadData];

        }

    }];

}
当视图第一次出现时,
UIAlertView
自动获取呼叫并关闭,然后当我点击第二次警报的OK按钮时,第二次获取呼叫,然后再次出现第一次
UIAlertView
。在过去的4个小时里,我一直在努力解决这个问题,这里给出的一些答案也不符合我的工作环境。谁能告诉我哪里错了


谢谢

您的第二个警报与第一个警报重叠,在解除第二个警报后,第一个警报将重新出现在屏幕上

-(void)viewDidAppear:(BOOL)animated
{
    UIAlertView * alertForCoordinate=[[UIAlertView alloc]initWithTitle:@"Coordinate" message:[NSString stringWithFormat:@"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
    alertForCoordinate.tag = 1;
    [alertForCoordinate show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0 && alertView.tag == 1) {
        [SVGeocoder reverseGeocode:CLLocationCoordinate2DMake(self.userLocation.coordinate.latitude, self.userLocation.coordinate.longitude)
                        completion:^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error)
         {

             if (placemarks.count>0)
             {

                 CLPlacemark * placemark=[placemarks objectAtIndex:0];
                 currentLocationUpcoming =[placemark locality];
                 /*****************Second Alert******************/
                 UIAlertView * alert=[[UIAlertView alloc]initWithTitle:@"Location" message:[NSString stringWithFormat:@"currentLocation==>%@",currentLocationUpcoming] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                 [alert show];

                 [self.eventsTableView reloadData];

             }

         }];
    }
}

我也有同样的问题。为了克服这个问题,我做了以下几件事。将alertview作为全局变量获取。然后呢,

if(!alertForCoordinate){
       alertForCoordinate=[[UIAlertView alloc]initWithTitle:@"Coordinate" message:[NSString stringWithFormat:@"latitude==>%f\nlongitude==>%f",self.userLocation.coordinate.latitude,self.userLocation.coordinate.longitude] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];        
       [alertForCoordinate show];
}
之后,当您在Alertview中单击“确定”按钮时。将其设置为
nil
。如下图所示

alertForCoordinate = nil;

希望,这将对您有所帮助。

您实际上没有第一个
UIAlertView
显示两次-动画只是让它看起来像这样,因为您正在连续显示两个
UIAlertView
s。您可能会看到警报1->警报2->点击警报2->警报1上的“确定”。这是因为如果显示多个,则
UIAlertView
s会堆叠起来

可以这样想:警报1首先显示,但仅在很短的时间内设置动画,直到被警报2显示中断。解除警报2会再次在屏幕上设置警报1的动画,即使它仍然是相同的警报,但看起来它会显示两次

尝试以下简单的测试用例:

-(void)viewDidAppear:(BOOL)animated
{
    [[[UIAlertView alloc] initWithTitle:@"Alert #1" message: delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    [[[UIAlertView alloc] initWithTitle:@"Alert #2" message: delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
您将在几秒钟内看到“警报1”,然后“警报2”将保留在屏幕上。点击“警报2”上的“确定”将再次显示“警报1”,因为它在堆栈上低于警报2

我也有同样的问题“UIAlertView显示两次”,但在我的例子中,这是因为长按手势识别器的编码错误(与原始问题不同,但可能对其他人有用,谷歌UIAlertView显示两次)。在处理长按的方法中,我调用了显示UIAlertView的方法。我的错误是没有回忆每次用户长按时会调用handle long press方法两次(一次用于按住,一次用于用户松开手指)。正确的代码识别手势状态(下面的代码)


这可能是在main.storyboard中复制一个按钮,从而复制其iAction引用的情况

我在main.storyboard中有一个样式完美的按钮,所以我用alt-drag复制了它,并在viewController上附加了一个新的iAction。我没有意识到这一点,但当我点击新按钮时,它为第一个按钮和第二个按钮启动了IBaction,因为它连接到了这两个按钮,因为它是第一个按钮的复制品


我右键单击main.storyboard中的第二个按钮,从第一个按钮和第二个按钮看到对iAction的引用,然后删除了对第一个按钮的引用。问题解决了。真是一场噩梦。希望这对其他人有所帮助。

是否调用了两次
viewdide
呢?没有,我在方法中设置了断点,有两个UIAlertView,它会正常显示两次。@KudoCC-我编辑了这个问题,请检查它,因为在创建视图后调用了viewdide(使视图可见)它可能有助于我们了解如何创建和添加此特定视图的视图层次结构。也许你添加到子视图中,并将其显示为模态视图(只是猜测…)请仔细阅读我所说的第一个UIAlertView的问题。我已经知道我有两个AlertView bro;)类似于sathiamoorthy。我想知道为什么不去黑。因为使用一个bool变量,我也可以实现它。不是吗。我想知道我哪里做错了。这就是问题所在。这不是黑客行为,因为UIAlertView是一个在屏幕上显示的控件,它有自己的优先级,所以当您在viewController中调用[alert show]方法时,它会显示并禁用viewController上的所有交互(UIAlertView上的按钮除外)。在你的代码中,同样的事情发生了,第二个警报视图占据所有优先级并禁用第一个警报,当你解除第二个警报时,优先级将转移到第一个警报。但我仍在搜索发生这种情况的原因。我知道你的答案可能有用,但它并不能解决我的问题:)这可能是一个解决方案,但为什么我要为这个小东西设置全局变量?那么这个变量将在整个文件生命周期中持续存在。是吗?是的。你是对的。但有时同样的问题不会发生。因为iOS 7。我认为是这样。我们没有解决这个问题的办法。不是吗?我是用iOS 6开发的,而不是用iOS 7开发的,因为这款应用非常适合iOS 6。我真的很抱歉。你应该试试其他的解决办法。@sathiamoorthy-是的,我用了你的解决办法,但运气不好。我怀疑某些内部调用会两次先调用UIAlertView。顺便说一句,感谢您的支持亲爱的:)解决方案,以防止它?尽管你的定义非常棒,所以我投票支持你:)不要连续显示两个警告视图。这不是一个好的用户体验,苹果可能会建议不要这样做。考虑使用第一个警报的内容来制作一个<代码> NSstring */COD>,然后在第二个警报当前发生的地方,呈现一个带有两个字符串的警报。不过,一般来说,如果可能,尽量避免使用
UIAlertView
s。
-(IBAction)handleHoldOnContact:(UILongPressGestureRecognizer*)sender {

    if (sender.state == UIGestureRecognizerStateBegan){
        // NSLog(@"UIGestureRecognizerStateBegan hold");
        //Do Whatever You want on Began of Gesture
        // ...            
    } else if (sender.state == UIGestureRecognizerStateEnded) {
        // NSLog(@"UIGestureRecognizerStateEnded release");
        //Do Whatever You want on End of Gesture
        // ...
    }
}