adlyy
adlyy

Reputation: 21

how to do things between two UIAlertView (ios7)

I define a UIAlertView, its tag = 101, to determine save or not, show another UIAlertView called alertView2 when clicked save button, and then delete the subviews of rootView. But when I call clear Code [self clearAllSubviewsInRootView];here, it clear subViews before call alertView2. How do I fix it?

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

Upvotes: 0

Views: 269

Answers (3)

Maniganda saravanan
Maniganda saravanan

Reputation: 2198

This works for me,.. And it so simple you can use this for an infinite alertview.

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

 }

}

Upvotes: 0

auco
auco

Reputation: 9579

UIAlertViews are modal views, but that does not mean they are synchronous.

In fact, UIAlertViews are asynchronous modal views.

In plain english, that means that they will be presented on screen, but some other code may be executed simultaneously (= asynchronous). So, code execution does not stop after calling [myAlert show]. The user however, is not able to select something else, he or she must deal with this one and only element on screen (= modal).

Having said that: I do not know the exact implementation of UIAlertViews, but it would not surprise me if the current runloop is being run to the end until the alert is actually presented on screen. That means, all code after [alertView show] will be executed to the end, and only then the alert will show (with the next runLoop).

So, you are asking "why it clears subviews before showing the second alert", but this is exactly what you tell it to do:

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

You are creating and showing the second view and then immediately call [self clearAllSubviewsInRootView] after [alertView show].

If you want to clearAllSubviews only after the user has selected something in the second alert view, you must move this call [self clearAllSubviewsInRootView]to a later point in your if (alertView.tag == 102) routine

Upvotes: 1

Jakub
Jakub

Reputation: 13860

The main problem here js you override name of the alertView inside method:

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

You have here property named alertView. Later you declare:

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

This is a bit confusing and i'm not sure what are you trying to achieve here.

But GCD seems to fitting your problem so Apple provide here a useful snippet (you can call it by start writing 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)
});

In your case you probably want (i'm not sure) to keep second alert view after code is completed. In this case you should

Upvotes: 0

Related Questions