我定义了一个UIAlertView,它的标签= 101,以确定是否保存,在单击保存按钮时显示另一个名为alertView2
的UIAlertView,然后删除rootView的子视图。但是当我在这里调用清除代码[self clearAllSubviewsInRootView];
时,它会在调用alertView2之前清除子视图。我该如何解决?
- (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 :@"当事人"];
}
}
}
}
答案 0 :(得分:1)
UIAlertViews是模态视图,但这并不意味着它们是同步。
事实上, UIAlertViews是异步模态视图。
简单来说,这意味着它们将在屏幕上显示,但其他一些代码可能同时执行(=异步)。因此,在调用[myAlert show]后,代码执行不会停止。然而,用户无法选择其他内容,他或她必须在屏幕上处理这个唯一的元素(=模态)。
话虽如此:我不知道UIAlertViews的确切实现,但如果当前的runloop运行到最后,直到警报实际显示在屏幕上,我才会感到惊讶。这意味着,[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
您正在创建并显示第二个视图,然后在[self clearAllSubviewsInRootView]
之后立即致电[alertView show]
。
如果您只想在用户在第二个提醒视图中选择内容后清除所有视图,则必须将此通话[self clearAllSubviewsInRootView]
移至if (alertView.tag == 102)
例程中的稍后位置
答案 1 :(得分:0)
这里的主要问题是你覆盖了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似乎适合你的问题所以Apple在这里提供了一个有用的片段(你可以通过开始写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)
});
在您的情况下,您可能希望(我不确定)在代码完成后保留第二个警报视图。在这种情况下你应该
答案 2 :(得分:0)
这对我有用,..它非常简单,您可以将它用于无限警报视图。
- (void)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
}
}
}