UIAlertController how to add 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 is 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 you can pass the value of this tag

alertView.tag = tag;

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

+4
source share
3 answers

UIAlertController- this UIViewControlleris why we need to assign a tag to view, so 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];
+5
source

create a property UIAlertController *alertController;and then use alertControllerwherever you want. set the tag as follows

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

alertController, alertController

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

I do not have a tag property in UIAlertController. You can use the block to get the 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 the tag this way -

alert.view.tag = 1;
+1
source

Source: https://habr.com/ru/post/1668233/


All Articles