2013-12-23 28 views
0

我定义了一个UIAlertView,它的标记= 101,确定是否保存,当单击保存按钮时显示另一个名为alertView2的UIAlertView,然后删除rootView的子视图。但是当我在这里调用清除代码[self clearAllSubviewsInRootView];时,在调用alertView2之前清除子视图。我如何解决它?如何在两个UIAlertView之间执行操作(ios7)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 101) 
    { 
     if (buttonIndex == 0) 
     { 

     } 
     else 
     { 
      if (buttonIndex == 1) 
      { 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"input fileName" message:nil delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; 
       alertView.tag = 102; 
       alertView.alertViewStyle = UIAlertViewStylePlainTextInput; 

       [alertView show]; 
      } 
      [self clearAllSubviewsInRootView]; 
     } 
    } 
    if (alertView.tag == 102) 
    { 
     if (buttonIndex == 0) 
     { 

     } 
     else 
     { 
      NSArray *viewArray = [self.canvasView subviews]; 

      NSUserDefaults *UD = [NSUserDefaults standardUserDefaults]; 
      NSString *scaleStr = [UD objectForKey:@"scale"]; 
      NSArray *dataArray = [NSArray arrayWithObjects:scaleStr, _labelArrivalTime.text, _textAccidentLocation.text, 
            _textDeclare.text, _textWeather.text, _textRoadSurface.text, [NSNumber numberWithFloat:canvasSize], nil]; 
      NSMutableArray *array = [NSMutableArray arrayWithObjects:viewArray, dataArray, nil]; 
      NSData * encodedata=[NSKeyedArchiver archivedDataWithRootObject:array]; 

      NSString *fileName = [alertView textFieldAtIndex:0].text; 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *floerName = @"file"; 
      NSString *saveDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:floerName]; 
      NSString *filePath = [saveDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.rta", fileName]]; 

      NSFileManager *fileManager = [NSFileManager defaultManager]; 
      if ([fileManager fileExistsAtPath:filePath]) 
      { 
       UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"file existed" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
       alertView.tag = 103; 
       [alertView show]; 
      } 
      else 
      { 
       [encodedata writeToFile:filePath atomically:YES]; 
       [self saveImage:_prospector.image :filePath :@"勘查员"]; 
       [self saveImage:_draftman.image :filePath :@"绘图员"]; 
       [self saveImage:_person.image :filePath :@"当事人"]; 
      } 
     } 
    } 
} 

回答

1

UIAlertViews是模式的看法,但是,这并不意味着他们是同步

事实上,UIAlertViews是异步模态视图

用纯英文表示它们将在屏幕上显示,但其他代码可能会同时执行(=异步)。因此,在调用[myAlert show]之后,代码执行不会停止。然而,用户不能选择其他的东西,他或她必须在屏幕上处理这个唯一的元素(=模态)。说了这么几句:我不知道UIAlertViews的确切实现,但是如果当前的runloop运行到最后直到alert实际出现在屏幕上,它并不会让我感到惊讶。这意味着,[alertView show]之后的所有代码都将被执行到最后,并且只有警报才会显示(使用下一个runLoop)。

所以,你问“为什么它显示了第二警报之前清除子视图”,而这正是你分不清什么做:

if (buttonIndex == 1) 
     { 
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"input fileName" message:nil delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; 
      alertView.tag = 102; 
      alertView.alertViewStyle = UIAlertViewStylePlainTextInput; 

      [alertView show];    // <-- you show the alert 
     } 
     [self clearAllSubviewsInRootView]; // <—- and clear all views 

你正在创建和显示第二查看并在[alertView show]后立即致电[self clearAllSubviewsInRootView]

如果你想在用户选择后,才在第二次警报视图东西clearAllSubviews,你必须在这个调用[self clearAllSubviewsInRootView]移动到后点你if (alertView.tag == 102)日常

0

主要的问题在这里的js你内心方法覆盖alertView的名字:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 

你这里有属性命名alertView。稍后你声明:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"input fileName" message:nil delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil]; 

这有点令人困惑,我不知道你在这里试图达到什么。

但GCD似乎适合您的问题,所以苹果在这里提供一个有用的片段(你可以开始写dispatch_after称呼它):

double delayInSeconds = 2.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    //put code here, whatever you want to fire later (after two seconds in this case) 
}); 

你的情况,你可能想(我不知道),以代码完成后保留第二个警报视图。在这种情况下,你应该

+0

虽然延迟应该可以解决问题(见我的回答如下),以我的经验延迟解决问题就像使用胶带固定你的汽车。它最终会脱落,并且不会阻止生长在...下面的锈... – auco

0

这对我的作品,..它如此简单的你可以使用这个无限的alertview。

- (空)alertView:(UIAlertView中*)alertView clickedButtonAtIndex:(NSInteger的)buttonIndex {

if ([alertView.title isEqualToString:@"First Alertview Title"]) { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 

     // do your stuff in cancel button --->First Alert 

    } else if (buttonIndex == 1) { 

     // do your stuff in other button ----> First Alert 
    } 

} else if ([alertView.title isEqualToString:@"Second Alertview Title"]) { 

    if (buttonIndex == [alertView cancelButtonIndex]) { 

     // do your stuff in cancel button ----> Second Alert 

    } else if (buttonIndex == 1) { 

     // do your stuff in other button -----> Second Alert 
    } 

} 

}

相关问题