saravanar
saravanar

Reputation: 649

UIAlertController how add the tag value in obj c

Using this for UIAlertView

- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
    alertView.tag = tag;
    [alertView show];
}

But now UIAlertView get deprecated. change my code

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:MyAlert];
   [self presentViewController:alertController animated:YES completion:nil];

Here how can pass this tag value

alertView.tag = tag;

Help how to pass the tag value in UIAlertController. Thanks advance.

Upvotes: 9

Views: 5590

Answers (3)

Antony Raphel
Antony Raphel

Reputation: 2078

create a property of UIAlertController *alertController;, then use this alertController where ever you want. set the tag like this

alertController.view.tag = <YOUR TAG VALUE>;

to get the tag of that alertController, when you click on YES on alertController

//OK button tapped.
[self dismissViewControllerAnimated:YES completion:^{
    NSInteger *tag = alertController.view.tag;
}];

Upvotes: 4

Anbu.Karthik
Anbu.Karthik

Reputation: 82779

UIAlertController is the UIViewController , so we need to assign the tag for view, soe use alertController.view.tag.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"sds" message:@"sdf" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = tag;
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];

update

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"sds" message:@"sdf" preferredStyle:UIAlertControllerStyleAlert];
alertController.view.tag = 3;
UIAlertAction* MyAlert = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                          {
                              // OK button tappped.
                              [self dismissViewControllerAnimated:YES completion:^{

                              }];

                          }];
[alertController addAction:MyAlert];
[self presentViewController:alertController animated:YES completion:nil];

Upvotes: 9

Mohammad Rana
Mohammad Rana

Reputation: 173

Have not any tag property in UIAlertController. You can use block for getting button action.

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert"
                                                               message:[NSString stringWithFormat:@"Your message"]
                                                        preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            //NSLog(@"OK");
        }]];
[self presentViewController:alert animated:YES completion:nil];

But you can use tag in this way-

alert.view.tag = 1;

Upvotes: 1

Related Questions