gpt4 book ai didi

ios - 如何在两个 UIAlertView 之间做事(ios7)

转载 作者:行者123 更新时间:2023-11-29 03:23:51 27 4
gpt4 key购买 nike

我定义了一个UIAlertView,它的tag = 101,用来判断是否保存,点击保存按钮时显示另一个名为alertView2的UIAlertView,然后删除rootView的 subview 。但是当我在这里调用 clear Code [self clearAllSubviewsInRootView]; 时,它会在调用 alertView2 之前清除 subview 。我该如何解决?

- (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 :@"当事人"];
}
}
}
}

最佳答案

UIAlertView 是模态 View ,但这并不意味着它们是同步

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

用简单的英语来说,这意味着它们将显示在屏幕上,但其他一些代码可能会同时执行(= 异步)。因此,代码执行不会在调用 [myAlert show] 后停止。然而,用户无法选择其他内容,他或她必须处理屏幕上的这个且唯一的元素(= 模态)。

话虽如此:我不知道 UIAlertViews 的确切实现,但如果当前 runloop 一直运行到最后直到警报实际显示在屏幕上,我不会感到惊讶。这意味着,[alertView show] 之后的所有代码都将执行到最后,然后才会显示警报(在下一个 runLoop 中)。

因此,您在问“为什么在显示第二个警报之前清除 subview ”,但这正是您告诉要做的:

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

您正在创建并显示第二个 View ,然后在 [alertView show] 之后立即调用 [self clearAllSubviewsInRootView]

如果您只想在用户在第二个警报 View 中选择某些内容后才清除所有 subview ,则必须将此调用 [self clearAllSubviewsInRootView] 移到 if (alertView.标签 == 102) 例程

关于ios - 如何在两个 UIAlertView 之间做事(ios7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20740496/

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