1

I have a UITextField and save button when the user press save i want to popup an alert to confirm whether he want to save and wait for his response. But unfortunately it seems the alert view show doesnt stop execution or wait for user response. so how can i implement such a situation.

- (IBAction)Savebuttonaction:(UIButton *)sender
{
    UIAlertView *view=[[UIAlertView alloc]initWithTitle:@"message" message:@"Do you want to save" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
    [view show];
    if(_isSaveConfirm)
        NSLog(@"Saved");
    else
        NSLog(@"Not Saved");
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        _isSaveConfirm=YES;
    }
    else
    {
        _isSaveConfirm=NO;
    }
}

please note i cannot write the further steps in the alertview delegate method. so please provide an alternate solution.

Raon
  • 1,266
  • 3
  • 12
  • 25
  • Update and elaborate more about your functionality your question with code whatever you tried so far. – Tirth Oct 04 '13 at 05:42
  • actually i just simplified the question has to save lot of details. so i have to make a simple code just wait – Raon Oct 04 '13 at 05:44
  • ok sure take your time and edit. – Tirth Oct 04 '13 at 05:45
  • please dont vote down! – Raon Oct 04 '13 at 05:54
  • Is that a school assignment? – Aqueel Oct 04 '13 at 06:16
  • Why are you not able to amend alertView:clickedButtonAtIndex:? By the way, I'm not sure if you think that execution _should_ pause when the alertView is shown and wait for the user's action; it definitely should not do that. – JoeFryer Oct 04 '13 at 08:10
  • maybe you can post some notification in the delegate method, and your current view controller can catch the notification for the next movement. – tassar Dec 09 '13 at 14:59

3 Answers3

0

You should handle user response like this.

- (IBAction)Savebuttonaction:(UIButton *)sender
{
    UIAlertView *view=[[UIAlertView alloc]initWithTitle:@"message" message:@"Do you want to save" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
    [view show];

}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        // put your logic to save
        NSLog(@"Saved");
    }
    else
    {
        // if you want handle this response
        // put your logic here, otherwise 
        // don't handle it.
        NSLog(@"Not Saved");
    }
}
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43
  • unfortunately i cannot do that! ...please read the question i cannot implement the save logic in the delegate method and i need the control back in the save button action – Raon Oct 04 '13 at 06:01
0

Have you tried a BlocksKit?

[UIAlertView showAlertViewWithTitle:@"message"
                            message:@"Do you want to save"
                  cancelButtonTitle:@"Yes" otherButtonTitles:@[@"No"] 
                            handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                      if (buttonIndex == 0) {
                          NSLog(@"Saved");
                      } else {
                          NSLog(@"Not Saved");
                      }
                  }];
AlKozin
  • 904
  • 8
  • 25
0

Do you want this?

- (void)buttonClicked:(id)sender {
    __block BOOL userClicked = NO;
    NSLog(@"Show alert");
    [UIAlertView showWithTitle:@"Synchronized Alert" message:@"" cancelButtonTitle:@"NO" otherButtonTitles:@[@"YES"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
        NSLog(@"Alert clicked");
        userClicked = YES;
    }];
    while (!userClicked) {
        NSDate* timeout = [NSDate dateWithTimeIntervalSinceNow:1.f];
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeout];
    }
    NSLog(@"Do something else");
    // ...
}

Codes above will log:

2013-12-09 20:21:07.612 [14728:60b] Show alert
2013-12-09 20:21:14.739 [14728:60b] Alert clicked
2013-12-09 20:21:14.775 [14728:60b] Do something else

If you want to use the normal UIAlertView instead of the block styled UIAlertView, the flag userClicked must be initialized as either a member variable or a global variable, so you can reset it in your delegate method.

Note: The above code will not work if you call method @sel(buttonClicked:) in a block and then assign the block to a dispatch queue, as @rob mayoff pointed out in NSRunLoop runMode does not always process dispatch_async, "If the queue is executing a block, no other block on that queue can start."

Community
  • 1
  • 1
wtl
  • 312
  • 3
  • 13