uialertview and delegate (delayed in code)

I'm checking if file already exists and if it is, I'm alerting the user if he wants o replace the file. I'm using alert view and a delegate. However when i run it using the simulator by the time the user selects YES or NO the program already run pass it and the blnVal has NO value regardless I'm not sure what i'm missing here.? (I searched the database here but couldn't find any related specific question)

  -(void) chkFile2Save
    {
        short tst;

        NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

        NSString* foofile = [documentsPath stringByAppendingPathComponent:pln2Save.text];
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];



        if(fileExists)
        {
            blnVal=NO;
           [self AskFileSave];
        }

        //blnVal always NO for whatever reason ... ?
        if(blnVal==NO)
            tst=5;
              //...
        else {
            tst=10;
            //..
        }


    }



    - (void) AskFileSave
    {
        UIAlertView *alertFileSave = [[UIAlertView alloc] initWithTitle:@"" message:@"File already exists.  Override the file with current data?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];

        [alertFileSave setTag:10];
        [alertFileSave show];
        [alertFileSave release];
    }

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


        //override file exists
        if([alertView tag]==10)
        {
            if(buttonIndex == 1)
            {
                blnVal=YES;
            }
            else
            {
                blnVal=NO;
            }

        }
    }

Answers


you are calling

[self AskFileSave];

that will execute

- (void) AskFileSave

than the execution will go back to :

//blnVal always NO for whatever reason ... ?
        if(blnVal==NO)
            tst=5;
              //...
        else {
            tst=10;
            //..
        }

You should move that part of the code to delegate method, There you have the option what the user selected, because the UIAlerView will not stop the code execution.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //override file exists
    if([alertView tag]==10)
    {
        if(buttonIndex == 1)
        {
            blnVal=YES;
            tst = 10;
        }
        else
        {
            blnVal=NO;
            tst=5;
        }

    }
}

I hope it helps!


Need Your Help

SQL-CLR stored procedure doesn't return value

c# sql-server-2008-r2 sqlclr clrstoredprocedure

I looked through Stackoverflow and found almost identical question here. It was asked a year ago and nobody answered it yet. Maybe I'll be more lucky than user1038334 and somebody will help me.

iOS 5.0.1 : How to verify that the folder is marked as “Do not back up” for iCloud?

iphone ios ios4 icloud

I want to set Do not back up to my folder which is in Document Directory.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.