0

I'm updating an Objective-C application in iOS and since UIAlertView is deprecated I'm using UIAlertController with UIAlertAction for the OK and Cancel buttons. So I have the following function for the back button but I don't know how to handle the OK and Cancel buttons since the OK should let you go back to the previous screen and the Cancel should let you stay in the same screen. The following is my code so far.

- (void)backAction:(id)sender
{
    //do your saving and such here

    if ([doneBtn isEnabled])
    {
        //CHANGE 2018
        UIAlertController *alert = [UIAlertController alertControllerWithTitle: @"Alert" message:@"Signature will be lost. Do you want to continue?." preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *ok = [UIAlertAction actionWithTitle: @"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            //should go to the previous scree. 
            //[alert dismissViewControllerAnimated:TRUE completion:nil]; //I'm not sure if this only hides the alert or hides the entire screen 

        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle: @"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
            //should stay in the same screen 
            [alert dismissViewControllerAnimated:TRUE completion:nil]; //the same doubt 

        }];

        [alert addAction: ok];
        [alert addAction: cancel];

        [self presentViewController:alert animated:YES completion:nil];


        //UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Signature will be lost. Do you want to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil];
        //[alert show];
    }
rmaddy
  • 298,130
  • 40
  • 468
  • 517
Mariana
  • 1
  • 1
  • 4

4 Answers4

1

The statement [alert dismissViewControllerAnimated:TRUE completion:nil] will only dismiss the alert, because it's the top view controller on the screen.

So, you don't need to do anything else for your Cancel action.

For your Ok action, you have multiple actions to go back to the previous screen :

  • If your view controller is a UIViewController alone, simply call [self dismissViewControllerAnimated:TRUE completion:nil]`

  • If the view controller is embedded into a navigation controller, call [self.navigationController popToRootViewControllerAnimated:YES].

  • You also can use an unwind segue to the view controller you want to go back too.

I hope it will help you! ;)

Lodoss
  • 439
  • 4
  • 11
0

It is not necessary to call [alert dismissViewControllerAnimated:TRUE completion:nil];, when you tap on one of the button the alert is dismissed.

In the completion of your ok button if your viewController is in embedded in a navigationController you can use [self.navigationController popToRootViewControllerAnimated:YES];.

Kerberos
  • 3,656
  • 3
  • 31
  • 51
0

The above answers are correct. I actually wanted to comment but I don't have enough reputation.

You should try to take a look at this thread to get a better understanding of dismissing a viewController. Dismissing a Presented View Controller

Victor Kwok
  • 104
  • 12
0

Try this, hope this will solve your issue.

UIAlertController * alert=[UIAlertController alertControllerWithTitle:@"Title" 
message:@"Message"preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* okButton = [UIAlertAction
                    actionWithTitle:@"Ok, please"
                    style:UIAlertActionStyleDefault
                    handler:^(UIAlertAction * action)
                    {
                        [self okButtonPressed];

                    }];
UIAlertAction* cancelButton = [UIAlertAction
                        actionWithTitle:@"No, thanks"
                       style:UIAlertActionStyleDefault
                       handler:^(UIAlertAction * action)
                       {
                           [self cancelButtonPressed];

                       }];

[alert addAction:okButton];
[alert addAction:cancelButton];

[self presentViewController:alert animated:YES completion:nil];

Now Dismiss the viewcontroller.

- (void)cancelButtonPressed{
 // Write your implementation for Cancel button here.
}

 - (void)okButtonPressed{
//Write your implementation for Ok button here
}
Mukesh
  • 724
  • 6
  • 18