iDeveloper iDeveloper - 3 months ago 22
iOS Question

Show Multiple Alert Controller on particular button click?

I have a label which asks a questions and user needs to answer by tapping on "YES" or "NO" button. if user clicks directly on NEXT Button the alert pops up saying

Choose Yes or No
if user selects Yes then he gets a new sub question there itself , if user clicks on NEXT without answering the sub question another alertView should pop up. On both the Condition only one alert view is displayed. help plz
heres the code

-(IBAction)btnNextClicked:(id)sender {

activeButton = sender;
if (([_txtOwnerShipPercentage.text isEqualToString:@" "] || _txtOwnerShipPercentage.text==nil || _txtOwnerShipPercentage.text.length==0) && (activeButton.tag == 1)) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Select Ownership Percentage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
} else if (activeButton.tag == 0){
UIAlertController *alertController2 = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Select 'YES' or 'No'" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController2 addAction:okAction];
[self presentViewController:alertController2 animated:YES completion:nil];

}
}


and i have set tags to YES and No Button... 1 & 2 respectively.
activebutton is nothing but globally declared UIButton

Answer

Here is the code of how you can manage two or multiple alert controllers in one context according to your requirement.

    //declare first alert controller
    UIAlertController *alertviewcontroller1 = [UIAlertController alertControllerWithTitle:@"My Alert 1"
                                                                   message:@"This is an action sheet of alert 1"
                                                            preferredStyle:UIAlertControllerStyleActionSheet];

    //declare second alert controller
    UIAlertController *alertviewcontroller2 = [UIAlertController alertControllerWithTitle:@"My Alert 2"
                                                                                  message:@"This is an action sheet of alert 2"
                                                                           preferredStyle:UIAlertControllerStyleActionSheet];

    //declare first alert controller action with hendler
    UIAlertAction *firstActionOfAlert1 = [UIAlertAction actionWithTitle:@"YES"
                                                          style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {

                                                              //Present second alert controller when user taps on 'YES' of first alert controller.
                                                              [self presentViewController:alertviewcontroller2 animated:YES completion:nil];
                                                          }];

    //declare second alert controller action with hendler
    UIAlertAction *firstActionOfAlert2 = [UIAlertAction actionWithTitle:@"YES"
                                                          style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {


                                                          }];

    //add both actions relatively
    [alertviewcontroller1 addAction:firstActionOfAlert1];
    [alertviewcontroller1 addAction:firstActionOfAlert2];

    //Present first alert controller
    [self presentViewController:alertviewcontroller1 animated:YES completion:nil];
Comments